SolidWorks Tips and Things

Subscribe | Daily Tips | Unsubscribe
SolidWorks Tips Home | 3D iDesign, Inc Home
#1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9 | #10 | #11 | #12 | #13 | #14| #15| #16|
#1 | #2 | #3 | #4 | #5 | #6 |
subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link
subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link

Part 1: Custom Properties Tutorial

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"
$PRP:"SW-Comments"
$PRP:"SW-Created Date"
$PRP:"SW-File Name"
$PRP:"SW-Folder Name"
$PRP:"SW-Keywords"
$PRP:"SW-Last Saved By"
$PRP:"SW-Last Saved Date"
$PRP:"SW-Short Date"
$PRP:"SW-Subject"
$PRP:"SW-Title"

Drawings Only

$PRP:"SW-Current Sheet"
$PRP:"SW-Sheet Format Size"
$PRP:"SW-Sheet Name"
$PRP:"SW-Sheet Scale"
$PRP:"SW-Template Size"
$PRP:"SW-Total Sheets"

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:

  1. 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). 
  2. 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.
  3. 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

 

  1. The last input required is the FieldValue.  Since we are looking for text, the input is the text from the array MyProp.
  2. 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:

 

 

 

 

SolidWorks Daily Text Tips

Chat here it stays so you can get updates each time you visit back.


Get your own Chat Box! Go Large!

Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs.
Product Design
2D to 3D
Art to Part
Reverse Engineering
Customer SolidWorks and PDMWorks Programming
Stress Analysis
CNC Programming

Current SolidWorks Service Pack
The current SolidWorks 2009 Service Pack is 1.0
The current SolidWorks 2008 Service Pack is 5.0
The current SolidWorks 2007 Service Pack is SP 5.0
The current SolidWorks 2007 x64 Service Pack is SP5.0
The current SolidWorks 2006 Service Pack is SP5.1
The current SolidWorks 2006 x64 Service Pack is SP5.1
The current PDMWorks 2006 Service Pack is SP5.1
The current SolidWorks 2005 Service Pack is SP5.0
The current PDMWorks 2005 Service Pack is SP5.0

Click here to login and download from SolidWorks Web site

Quick Links SolidWorks
3D iDesign, Inc Engineering Services
Video Cards Testing

Please support this site and donate any amount you would like if this site helped you out.

 

For more books visit here

New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today

About Us | Contact Us | ©2007 3D iDesign, Inc ©

SolidWorks and SolidWorks Applications are registered trademarks of SolidWorks Corporation. All other names may be trademarks of their respective owners. SolidWorks Tips and Things is not affiliated with or sanctioned by the SolidWorks Corporation