Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Property VB Help

Status
Not open for further replies.

RC2003

Mechanical
Feb 4, 2003
5
I need help with the macro I have created to simply the task of entering custom properties into the title block of a drawing. Presently the macro consists of only one custom property "Drawn By".

When I run the macro a dialog box pops up and allows me to enter data into the field, click the "ok" command button and the custom properties of the drawing is populated.

When I run the macro a second time the field in the dialog box is empty, but the drawing still contains the custom prop.

Problem
1. I want the field content to be displayed if it has already been filled in.
2. If I modify the field on the second running of the macro and click "ok" the drawing needs to update.

Please don't tell me to look at someone’s web site to solve the problem. I have scoured the web and spent a week trying to figure out the code from other macros.

Here's the Code

Const swCustomInfoUnknow = 0
Const swCustomInfoText = 30
Const SwCustomInfoDate = 64
Const swCustomInfoNumber = 3
Const swCustomInfoYesOrNo = 11

Private Sub UserForm1()

Dim swApp As Object
Dim Part As Object
Dim Gtol As Object
Dim Retval As Object
Dim sDrawn As String

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
sDrawn = Part.CustomInfo2("", "Drawn By")

Part.AddCustomInfo "Drawn", "Text", ""

Part.FileSummaryInfo
End Sub

Private Sub cmd_OK_Click()

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc

Retval = Part.AddCustomInfo3("", "Drawn By", swCustomInfoText, TxtDrawn.text)

Unload Me

End Sub
Private Sub cmd_cancel_Click()

Unload Me

End Sub

***
The userform has a label "Drawn By", textbox (Name is "drawn" and to command buttons. "Ok and Cancel"
 
Replies continue below

Recommended for you

I wrote a program to copy custom info from a selected file to the active file. I found that it was necessary to do first add the custom info and then overwrite the custom info. This is because if the custom info already exists, "ModelDoc2.AddCustomInfo3" does not overwrite, and it is necessary to do a "ModelDoc2.CustomInfo2".

[bat]You don't fight destiny, no sir... and you don't eat crackers in the bed of your future![bat]
 
I believe here's what your missing....

to fill you user form:

declare:
Dim sConfig As String

sConfig ="" 'not config specific
Userform1.(txtDrawn)=Part.CustomInfo2(sConfig, "DRAWN")

to replace existing value :
retval = Part.DeleteCustomInfo2(sConfig, "DRAWN")
retval = Part.AddCustomInfo3(sConfig, "DRAWN", swCustomInfoText, Userfrom1.txtDrawn)



Remember...
"If you don't use your head,
your going to have to use your feet."
 
RC2003
030203usf_prv.gif


I cannot tell from your source or question, but normally - Custom Properties are applied to a model and then used in a Drawing. If this is what you are doing than I can help a little - I think. At times, SW is a little slow at updating the information in the drawing and it has to be forced by doing a Ctrl-B or a Ctrl-Q.

Assuming that you are running the macro in a Model (Part of Assembly) the largest problem that most people have is that some CPs have been entered manually and the name of the CP is different - Your “Drawn By” CP could be “drawn by”, “drawn By”, “Drawn by”, or “DRAWN BY” - OR - any other combination. The following snippet eliminates this problem by comparing each CP with an array of CP names that you have established - Then deleting the original CP and replacing it with a Preferred CP Name. This snippet should be placed at the beginning of your macro.

Dim retval As Boolean
Dim ConfigName As String

Dim Part As Object
Dim PreferredNames As Variant
Dim PropertyNames As Variant
Dim FieldData As Variant
Dim FieldType As Variant

Set Part = swApp.ActiveDoc ' Initialization

'Place the CP names that are preferred in this array
PreferredNames = Array("Date", "Status", "Dwg No")
PropertyNames = Part.GetCustomInfoNames() 'An Array of CP Names
For Each FieldName In PropertyNames 'Loop through each CP Array
For Each PreferredName In PreferredNames 'Loop through the Preferred Array
'Test to see if the CP is in the Preferred List
If StrConv(PreferredName, vbUpperCase) = StrConv(FieldName, vbUpperCase) Then
FieldData = Part.CustomInfo2("", FieldName) 'Save the Field Data
FieldType = Part.GetCustomInfoType3("", FieldName) 'Save the Field Type
retval = Part.DeleteCustomInfo2("", FieldName) 'Delete the CP
retval = Part.AddCustomInfo3("", PreferredName, FieldType, FieldData) 'Recreate It
End If
Next PreferredName
Next FieldName

Next - Test each CP and stuff it into a variable if it exists

If (Part.CustomInfo2(ConfigName, "Drawn By") = "") Then
Drawn = "Ralph" 'Give it a default value incase it doesn't exist
Else
Drawn = Part.CustomInfo3 (ConfigName, "Drawn By")
End If

Next - Run your form - If it does not exits with an OK - do an Exit Sub
If it does exit with an OK then stuff the values back into the CPs
(I try to maintain the field type - so another direct replacement works for me)

If (Part.CustomInfo2(ConfigName, "Drawn By") = "") Then
FieldType = Part.GetCustomInfoType3("", FieldName) 'Save the Field Type
retval = Part.DeleteCustomInfo2("", FieldName) 'Delete the CP
retval = Part.AddCustomInfo3("", "Drawn By", FieldType, Drawn) 'Recreate It
Else If
retval = Part.AddCustomInfo3 ("", "Drawn By", 30, Drawn)
End If

You may need to force a rebuild - but I doubt it
I hope this helps

Random_Shapes_Pointed_shapes_prv.gif
Lee
Random_Shapes_Pointed_shapes_prv.gif


Never begin a vast project - with a half-vast idea
 
Thanks for your help, but I can't get it to work with any of your suggestions. (It’s probably my fault)

Keep in mind I know absolutely nothing about programming.
So if your recommending something, please paste in back into the code as part of your reply. If not I’m sure I’ll screw it up

StarrRider, I am trying to use this macro as a way to fill in the title block and other information on the drawing that's relevant to the part but relevant to the manufacturing process.

If anyone requires the actual macro please let me know.
Thanks for all your help
 
RC2003
030203usf_prv.gif


Most people put Custom Properties in a Model (a Part or an Assembly) and not in a Drawing. The reason for this is that that data can be - and normally is - used in several places. As an example: a Part Number or a Description may be used in the Drawings Title Block, but it is also used in the Bill Of Materials. If that data is place in the Drawing then it will only be available in the drawing and you will end up adding Custom Properties to the Model for your BOM's anyway.

You might think that some of that data will never be needed in any place except the drawing - and you might be right. On the other hand, your manager might ask you for a special listing - like Who created Which drawings that relate to project ABC. By placing the information in the Model, you'll be able to have that listing in minutes by creating a special BOM and inserting it into a drawing.

We made a similar decision several years ago. We inserted Excel spreadsheet into our drawings templates to handle Notes and ECO data. This looked like the easiest solution at the time. Later - we found that was not the case. Not only did changing a drawings size removed everything that had been entered - so did updating a template to accommodate changes to the company’s logo. I didn’t think about that when I wrote a macro that updated several thousand drawings over the weekend. Some things you just don't see coming until they slap you in the face.

The choice is your - but I wouldn't put anything into a drawing that I MIGHT someday need - or want to change.

Random_Shapes_Pointed_shapes_prv.gif
Lee
Random_Shapes_Pointed_shapes_prv.gif


Don’t wait!!!! Procrastinate now!
 
RC2003

I am having trouble running your code in SW2001+. I have never seen the use of Private Sub UserForm1(). I had to change it to Private Sub UserForm_Initialize(). Probably has something to do with the changes made in 2003.

Anyway, I can tell you why when you run the macro a second time you do not see value of the custom property in your text box. Change the line in your code from

sDrawn = Part.CustomInfo2("", "Drawn By")

to

TxtDrawn.text = Part.CustomInfo2("", "Drawn By")

Regg
 
RC2003

Unfortunately I am at home and only have SW2001+ installed. My work computer has SW2003. I looked over your code briefly earlier but I have now more time.

I have to agree with TheTick, if the custom property already exists, you should use "ModelDoc2.CustomInfo2" to set it to a new value. Also I believe the retval value for "ModelDoc2.AddCustomInfo3" is boolean and not an object as you have it (at least it is in SW2001+, I will check on Monday with SW2003). I am also curious about your statement: Part.AddCustomInfo "Drawn", "Text", "". Did you mean to create the custom property "Drawn" or should this have been "Drawn By"? I did learn one thing, I did not know you could use "Part.AddCustomInfo" as a Statement instead of a function as the API help indicates. In the end, with the changes I indicated, I was able to run your macro without problem.

Regg [smile]

 
Like Regg said:

Be sure your return variable is explicitly declared as a boolean and not a variant in cases where that is what the API method requires, such as for AddCustomInfo3.

ModelDoc2.CustomInfo2 is a set/get property that needs no return value.
 
Const swCustomInfoText = 30

Private Sub UserForm_Initialize()

Dim swApp As Object
Dim Part As Object

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc

TxtDrawn.Text = Part.CustomInfo2("", "Drawn By")

End Sub

Private Sub cmdOK_Click()

Dim swApp As Object
Dim Part As Object
Dim retval As Boolean

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc

retval = Part.DeleteCustomInfo2("", "Drawn By")
retval = Part.AddCustomInfo3("", "Drawn By", swCustomInfoText, TxtDrawn.Text)

Unload Me

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor