Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

API for SW-Mass@etc... 3

Status
Not open for further replies.

oorah

Mechanical
Jul 29, 2003
10
Does someone have the code to add a custom property, such as weight, and link it to the mass value in mass properties?

I have code to "GetMassProperties()" and display the mass value in a window or report, but I don't want to add a property and just paste the value. I would like to use the "AddCustomInfo2(...)" code to put the custom property AND the FORMULA that SW uses, i.e. "SW-Mass@"FileName" so that it will update the custom property value automatically.
 
Replies continue below

Recommended for you

Sub WriteProps()
Dim SelList As Variant 'property from lboProps.list
Dim PropList() As swPropParams
Dim TargetConfig As String
Dim i As Long

TargetConfig = CStr(TargetConfigNames(cboTgtCfgs.ListIndex))

'eliminate unselected items from list
'from bottom to top
For i = (lboProps.ListCount - 1) To 0 Step -1
If lboProps.Selected(i) = False Then lboProps.RemoveItem (i)
Next
'move selected items to SelList
SelList = lboProps.List
'ubound(SelList,1) returns -1 if list is empty
If UBound(SelList, 1) = -1 Then Exit Sub
'move SelList to PropList
ReDim PropList(LBound(SelList) To UBound(SelList))
For i = LBound(SelList) To UBound(SelList)
PropList(i).pName = CStr(SelList(i, 0))
PropList(i).pType = ciTypeNum(CStr(SelList(i, 1)))
PropList(i).pValue = CStr(SelList(i, 2))
Next
'move PropList properties to Target file
'Target = swApp.ActivateDoc2(TargetFileName, True, Errorss)
For i = LBound(PropList) To UBound(PropList)
boolstatus = Target.AddCustomInfo3(TargetConfig, PropList(i).pName, PropList(i).pType, PropList(i).pValue)
If chkOverwrite.Value = -1 Then
Target.CustomInfo2(TargetConfig, PropList(i).pName) = PropList(i).pValue
End If
Next
End Sub
 
Hope this helps. It's an excerpt from a program that copies custom info from one part to another.

Some points to remember:

1.) AddCustomInfo3 does not overwrite existing properties. If you need to overwrite, use CustomInfo2

2.) mass properties are dependent on the units setting in your mass properties pane, accessed by the options button on that pane, ("scale" icon or "Tools --> Mass Properties". They are not controlled by the units settings under "Tools --> Options --> Document properties".

[bat]All this machinery making modern music can still be open-hearted.[bat]
 
Thanks for the quick response, and this code appears to be exactly what I'm looking for. However, I'm getting a compile error; "User-defined type not defined" on line "Dim PropList() As swPropParams"
 
Sorry, that property is a bit out of context. It is a property defined with the VB keyword Type.


Public Type swPropParams 'used for writing custom info
pName As String
pType As Long
pValue As String
End Type


[bat]All this machinery making modern music can still be open-hearted.[bat]
 
Now I get a "Sub or function not defined" for "TargetConfigNames" in the following line of code:

TargetConfig = CStr(TargetConfigNames(cboTgtCfgs.ListIndex))
 
It's an excerpt from a (much) larger program. I posted this to give you an idea of how the syntax for copying custom info goes.

I didn't intend for this to be a stand-alone, run-on-its own routine. Sorry if that wasn't clear.

I have a simpler program somewhere, but I'll have to dig for it before I can post it. In the meantime, try browsing SBaugh's or berlitz's site to see if there's anything useful.

[bat]All this machinery making modern music can still be open-hearted.[bat]
 
Thanks TheTick. I figured that out shortly after sending my last reply. Your code will give me a starting point and I'll play around with it tomorrow. If I can get it to work, it will add a custom property called &quot;Weight&quot;, text type, with a value showing &quot;SW-Mass@<FileName>&quot;.
 
' ******************************************************************************
' D:\TEMP\swx263\Macro1.swb - macro recorded on 01/11/02 by HSENSCHWAR
' ******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long
Dim Annotation As Object
Dim Gtol As Object
Dim DatumTag As Object
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Dim CustInfo As Variant
Dim string1 As String
Dim retval As Variant
Dim numcust As Integer
Dim TempString As String
Dim Template As Object
Dim Errorss As Long
Dim TemplateTitle As String
Dim InfoType As Variant
Dim InfoContents As String

Sub main()

Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Set Part = swApp.ActiveDoc
string1 = &quot;Ready to COPY CUSTOM INFO from file &quot;
retval = swApp.SendMsgToUser2(string1, swMbInformation, swMbYesNo)

TempString = swApp.GetUserPreferenceStringValue(swDefaultTemplatePart)
retval = swApp.SendMsgToUser2(string1 + TempString, swMbInformation, swMbYesNo)
retval = swApp.SendMsgToUser2(TempString, swMbInformation, swMbOk)
If retval = swmbhitno Then End

Set Template = swApp.NewPart
TemplateTitle = Template.gettitle

CustInfo = Template.GetCustomInfoNames2(&quot;&quot;)
numcust = UBound(CustInfo)
retval = swApp.SendMsgToUser2(Str(LBound(CustInfo)) + &quot; to &quot; + Str(UBound(CustInfo)), swMbInformation, swMbOk)

For i = 0 To numcust
InfoType = Template.GetCustomInfoType3(&quot;&quot;, CustInfo(i))
InfoContents = Template.CustomInfo2(&quot;&quot;, CustInfo(i))
retval = Part.AddCustomInfo3(&quot;&quot;, CustInfo(i), InfoType, InfoContents)
Next i

swApp.QuitDoc (TemplateTitle)

retval = swApp.SendMsgToUser2(&quot;Finito!&quot;, swMbInformation, swMbOk)
End Sub
 
One of my early attempts at custom info manipulation. This one copies the custom info from the default part template to the active model.

[bat]All this machinery making modern music can still be open-hearted.[bat]
 
This is part of a program in VB that helps the user to input values for custom properties, which are used in BOM and title blocks.

'If document is PART find density of selected material in kg/m^3
'and attach it to the document
If docType = swDocPART Then
Select Case strMaterial
Case &quot;CRS&quot;
dblDensity = 7830
Case &quot;HRS&quot;
dblDensity = 7850
Case &quot;DELRIN&quot;, &quot;BLACK DELRIN&quot;, &quot;WHITE DELRIN&quot;
dblDensity = 1410
Case &quot;ALUMINUM&quot;, &quot;ALUMINUM ANGLE&quot;, &quot;6061-T6 ALUMINUM&quot;, &quot;ALUM TLG PLT&quot;
dblDensity = 2700
Case &quot;STEEL ANGLE&quot;, &quot;THREADED ROD&quot;
dblDensity = 7840
Case &quot;A2&quot;, &quot;O1&quot;
dblDensity = 7715
Case &quot;UHMW-PE&quot;
dblDensity = 930
Case &quot;NYLON&quot;
dblDensity = 1150
Case &quot;RUBBER&quot;
dblDensity = 1506
Case &quot;ALUMINUM BRONZE&quot;
dblDensity = 7702
Case &quot;POLYURETHANE&quot;
dblDensity = 1170
Case &quot;STAINLESS STEEL&quot;
dblDensity = 7920
Case Else
dblDensity = 7800 'generic steel
End Select


'assign density to part
retval = Part.SetUserPreferenceDoubleValue(swMaterialPropertyDensity, dblDensity)
'define mass string
strMassProp = Chr(34) & &quot;SW-Mass@@&quot; & strConfName & &quot;@&quot; & DocTitle & Chr(34)
'define Weight custom property
retval = Part.AddCustomInfo3(strConfName, &quot;Weight&quot;, swCustomInfoText, strMassProp)
Else
'for drawing and assembly documents
retval = Part.AddCustomInfo3(strConfName, &quot;Weight&quot;, swCustomInfoText, &quot;N/A&quot;)
End If

retval = Part.EditRebuild3

in my particular case I dont need to display the weight for an assembly so I just set it to &quot;N/A&quot;.

Keep in mind, as Tick said, that the Mass value will change as you change the units. That made me remove the mass info from the titleblock. It seems that in SW2004 the user can &quot;fix&quot; the units for the mass.

I use AddCustomInfo3 because I delete the existing property somewhere else in the program.
 
netshop21,
I created a case statement that put in the material into the title block, very similar to yours posted here. I wish I had written the code to read an outside text data base. That way anyone could edit it. Now I am the only one that can or wants to edit the program to add material. Now I do not have time to change the original program.


Bradley
 
Bradley

For read/input porperties or custom properties I use an Excel macro (not an SW macro). The properties names are listed in the first column, and the configurations names are listed in the first line. So each cell will have a certain property of a certain config.

This way I can combine the power of a macro with an easy edit of the cells (that is, properties) in Excel, without the need of digging deeper in VBA. There are tables, somewhere in the Excel sheet, with material and density, and other interesting data for our company, that can be selected for input in the related cells. Anyone with little skills in Excel can change these tables (for example, for use more materials) independently from code. This way I avoid the problems that Bradley is having.

Regards
 
macPT,
You have a very good idea. Wish I had heard about this, back when we started SolidWorks. Sounds a lot easier than what I did. It is to late for us now.
The only fear that I have now, is that SolidWorks will dump Excel for their new Bill of Material table. We now have a lot of code built into our Excel Bill of Material to set quantities.


Bradley
 
MacPT,

I used the code from netshop21 and it works fine. Your Excel macro sounds interesting though... You are not inserting a Design Table, correct? Do you have a spreadsheet I can look at?
 
oorah

No it's not a Design Table. I explain you a litle bit what I do.

1 - I have an independent Excel file (lets say custprop.xls) with a proper macro
2 - I create a part or assembly in SW
3 - I click, in custprop.xls, the button &quot;read properties&quot;, and all the properties of the active part/assembly (and of their configurations) are printed in rows and columns (of course that, in the begining, they are all empty) and the screen as the following aspect:

|base part|conf1|conf2|....|confn
--------------------------------------
prop1| | | | |
--------------------------------------
prop2| | | | |
--------------------------------------
.....| | | | |
--------------------------------------
propn| | | | |
--------------------------------------
4 - Now I can fill these cells with the intended data. I can copy/paste between configurations or even between two different parts. I have also input controls and input tables in some props using the proper Excel functions.
5 - I click, in the custprop.xls, the button &quot;update properties&quot; and the information in 4- is writed in the SW file
6 - I save the SW file. There is no need to save custprop.xls (in fact, that should be avoided)
7 - Next time I do 3- in this file, the information that Excel shows is the last in 4-. I can easely modify any cell and do 5- and 6- again to update the custom properties.

I gess that the macro is similar to others that do this job inside SW (the API for properties management will be the same). The trick is that the nacro is in an Excel file and properties are printed in an Excel cell instead of a VBA window. This way I can use the Excel power to edit cells instead of hard programing in VBA to edit the props.

Regards

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor