Chat here it stays so you can get updates each time you visit back. |
|
Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs. |
Current SolidWorks Service Pack |
|
Quick Links SolidWorks |
Please support this site and donate any amount you would like if this site helped you out.
|
|
|
New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today
|
API Tip #3 10/22/2001 updated 11-01-01
One of the most often overlooked areas of SolidWorks is the custom property. In this example we will look at creating custom properties from a program.
The first step will be creating custom properties and inserting them into a document. The methods used are valid for parts, assemblies or drawings. The two most common locations for custom properties are in parts and drawings.
Part1: Setting Properties
Initial Code
SolidWorks does not record actions performed in some dialog boxes through macros. File properties are set only in a dialog box. Therefore, start and stop recording a macro without performing any actions. Save the file as properties.swp. Edit the macro we just created and remove the extra information so we're left with the following:
Public swApp As Object
Public Part As Object
Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
End Sub
Setting Properties
Let's start by populating some general properties. Add the following code to your macro.
Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Dim MyProp (1, 3) As String
'Property names
MyProp(0, 0) = "LastSavedBy"
MyProp(0, 1) = "CreatedOn"
MyProp(0, 2) = "Revision"
MyProp(0, 3) = "Material"
'Property values
MyProp(1, 0) = "$PRP:" & Chr(34) & "SW-Last Saved By" & Chr(34)
MyProp(1, 1) = Date
MyProp(1, 2) = "A"
MyProp(1, 3) = "A36 STEEL"
Dim m As Integer
For m = 0 To 3
retval = Part.AddCustomInfo3("", MyProp(0, m), 30, _
MyProp(1, m))
Next m
End Sub
Again, we have made use of arrays for our property names and the corresponding values. We've also linked to some other automated functions and properties. SolidWorks has several properties you can link to. One of which is $PRP:"SW-Last Saved By". This will always link to the login name of whoever saved the SolidWorks file last. The following list may be helpful in creating links to SolidWorks links.
All Documents$PRP:"SW-Author" |
Drawings Only$PRP:"SW-Current Sheet" |
In creating this link we have used a VB character function with the code Chr(34). The reason for this is that quotes mean something specific in VB. They start and finish a string definition. However, we need to have quotes in our string. Chr(34) passes a quote character to the string without terminating the string definition. Other helpful characters are listed below.
Character Code Return Value
Chr(8) backspace
Chr(9) tab
Chr(13) carriage return
Chr(34) "
Chr(38) &
Chr(176) °
Chr(177) ±
Chr(216) Ø
For MyProp(1, 1), we have used the VB Date function. This function returns the current date in mm/dd/yyyy format.
Last, we added a For.Next statement or loop that fills in all of the properties using the Part.AddCustomInfo3 SolidWorks method.
retval = ModelDoc.AddCustomInfo3 ( configuration, FieldName, FieldType, FieldValue )
The AddCustomInfo3 method has the following structure:
- The first item required is the string name of a configuration. Custom properties in parts and assemblies can be configuration specific. If so, you must specify which configuration to place the properties into. If this is left as an empty string as we have done, the custom properties of the entire part are populated (not configuration specific).
- The next input item is the FieldName or name of the property. The first one in our example is "LastSavedBy". This is defined in our MyProp array.
- The FieldType input tells SolidWorks if our value is text, number, date or yes/no. Text is what we are using for all of our values (including the date). It can hold any text information. The only time you might want to use the other types is to limit the user input to certain values. This input requires a long type number. In this case, the number 30 refers to text input. The following list gives reference to the other types defined in swconst.bas.
swCustomInfoText = 30
swCustomInfoDate = 64
swCustomInfoNumber = 3
swCustomInfoYesOrNo = 11
- The last input required is the FieldValue. Since we are looking for text, the input is the text from the array MyProp.
- Finally, the AddCustomInfo3 method can return a Boolean (true or false) value if desired. We are setting this returned value to the variable retval. That way we will be able to check if the method was successful or not.

Conclusion
Go ahead and run your macro at this point. It now fills out the properties we specified and their corresponding values. If you look at your file properties at this point they should look like the image shown.
Look for another installation soon that will describe how to add a graphical user interface or GUI to this simple macro.
Mike Spens
mikespens@solidworkstips.com
For more from this author look into:




