Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SW API - Another way to load the CustomPropertyManager object? 1

Status
Not open for further replies.

shawn76o

Nuclear
Nov 14, 2006
31
0
0
US
Hi all,

Bear with me here, I'm trying to learn how to use the SldWorks.CustomPropertyManager object, but the examples I've see in SW API help require the user to select a "weldment feature", and the API code uses a "SldWorks.SelectionMgr" object to set the "SldWorks.Feature" object, which finally sets the "SldWorks.CustomPropertyManager" object. (I hope that made sense...)
First part of question - am I understanding the SelectionManager object correctly? I understand it's "GetSelectedObject" method to mean "Get the object that the user has selected". Or does it mean "Get the programatically selected object"? If it means "user has selected", then I don't understand how this type of macro would be useful for custom properties. If the user has to manually select a feature, then open and run a macro to add a custom property, as shown in the example below, it seems it would be just as easy just to select a feature then click File/Properties, wouldn't it? Or did I miss the point?
Second part of question - Is there a way to load the CustomPropertyManager object without first "consulting" the SelectionManager object? Please see the code below to see what I'm referring to. I'm hoping there is a way to load the CustomPropertyManager with information from a Component2 object that's currently in memory..

Code:
This example shows how to add a custom property to and gets the custom properties assigned to a weldment feature.

 

[green]'------------------------------------------[/green]
[green]'[/green]
[green]' Preconditions:[/green]
[green]'        (1) Model document is open.[/green]
[green]'        (2) Weldment feature is selected.[/green]
[green]'[/green]
[green]' Postconditions: Custom property named Date added is added to the selected weldment feature.[/green]
[green]'----------------------------------------[/green]

Option Explicit

Sub main()

    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim swSelMgr            As SldWorks.SelectionMgr
    Dim swFeat              As SldWorks.feature
    Dim swCustPropMgr       As SldWorks.CustomPropertyManager

    Dim nRetVal             As Long
    Dim vNameArr            As Variant
    Dim vName               As Variant
    Dim bRet                As Boolean

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFeat = swSelMgr.GetSelectedObject5(1)
    Set swCustPropMgr = swFeat.CustomPropertyManager

    

    [green]'   Feature::GetTypeName[/green]
    [green]'       "SubWeldFolder"[/green]
    [green]'       "WeldmentFeature"[/green]
    [green]'   CustomPropertyManager::Add/GetType[/green]
    [green]'       FieldType[/green]
    [green]'           "Text"[/green]
    [green]'           "Date"[/green]
    [green]'           "Number"[/green]
    [green]'           "Yes or no"[/green]

    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  " & swFeat.Name & " [" & swFeat.GetTypeName & "]"

    

    [green]' Add custom property[/green]
    bRet = swCustPropMgr.Add("Date added", "Date", "27-apr-2004")

    [green]' Get all custom properties; Date added is the last one in the list
[/green]
    vNameArr = swCustPropMgr.GetNames: If IsEmpty(vNameArr) Then Exit Sub

    For Each vName In vNameArr

        Debug.Print "    " & vName & " [" & swCustPropMgr.GetType(vName) & "] = " & swCustPropMgr.Get(vName)

    Next vName

End Sub

[green]'------------------------------------------[/green]

See what I mean? Is there a way to set the CustomPropertyManager object WITHOUT first using the SelectionManager?
OR... can the SelectionManager's method "GetSelectedObject5" call an object that was PROGRAMATICALLY selected, as opposed to MANUAL selection by the user? [ponder]



Shawn Oelschlager
Production Control Ass't
Transco Products Inc.

SolidWorks Office Premium 2006 - SP4.1
Pentium 4 (2.8 GHz) - 2GB RAM
 
Replies continue below

Recommended for you

This is as common misunderstanding based on a (IMHO) poor choice of naming convention in SolidWorks. The CustomPropertyMgr is only for weldment custom properties, which are different from the custom properties you want. Although the SW user interface and help refers to what you want as custom properties, the API refers to them as CustomInfo. Check the API help on ModelDoc2::CustomInfo2. That is the way to get/set what you're trying to manipulate. There's a pretty good example in there too.

Good luck!
 
I meant to also answer your other question about GetSelectedObject, although it's moot for what you're currently trying to do.

GetSelectedObject is used to get the object that is currently selected in SolidWorks. It could be a feature in the tree, geometry on the model, a drawing view, etc. There are many times you want to write a macro that acts on things that the user pre-selects. It's easier and less resource-intensive than a macro that prompts the user to select the required items. You can also add or delete items from the set of selected items programmatically.

You would usually use GetSelectedObjectCount and GetSelectedObjectType prior to using GetSelectedObject to verify that the items that were selected are correct for what your macro expects.
 
Status
Not open for further replies.
Back
Top