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 Assembly selection

Status
Not open for further replies.

SeanF

Mechanical
Nov 26, 2002
26
I would like to check to see if an object selected is a component or a sub-asm via the API. I am having no luck. Things I think should work don't seem to. The following is really annoying:

retval = Component2.GetChildren ()
If this assembly component is a part document, SolidWorks returns NULL. If this assembly component is the root component or a subassembly, then this method returns the child components that belong to the assembly document.

' Get the selection manager class
Set SelMgr = Part.SelectionManager()
' Get the first item in the selection list
Set selObj = SelMgr.GetSelectedObject(1)

RetVal = selObj.GetChildren()

MyBool = IsNull(RetVal)

If MyBool = True Then
PrA2 = "A"
Else
PrA2 = "P"
End If

Sadly, the ret is NOT null if a component is selected. I have been trying to use this to determine if asm or component is selected. RetVal is declared as Variant(btw) at any rate, I don't get a NULL ret if a component is selected. I DO however, get a var array of the children if selected is asm.

I am stumped. Is the ret from SldWks not correct? Or am I not testing the value properly?

I am writing a macro to mate the def 3 planes. However, my planes are named different for asm's and parts. A_TOP and P_TOP respectivly. So, I need to determine if I have selected a part or asm when I run this macro as I have to use different names depending on the selection.

*sigh*
And it's only Monday....

TIA!

Sean F
Mechanical Engineer
seanf@newing-halll.com
1 2 many l's in e-mail
 
Replies continue below

Recommended for you

First don't call it RetVal. Call it something meaningfull, without the risk of confusion with other variables, like Children (declared as variant).

Then you can try the following (similar to what you have):
Children = selObj.GetChildren 'without the ()

Count how many children in the object:
ChildCount = UBound(Children) 'ChildCount As Integer

Finally test ChildCount:
If ChildCount=0 then you have a part.

Hope that helps.
 
How about using retval = Component2.GetModelDoc () ?

Now where the heck did I find that? Some of the API calls are not properly listed in the help index. Go to the help page for component2 and click the link for methods.
 
>> I don't get a NULL ret if a component is >>selected. I DO however, get a var array of >>the children if selected is asm.

Not exactly. You are checking to see if the current component has children. Returning an array does not guarantee the components in the array ARE assemblies, it only guarantees that the PARENT is an assembly.

In other words, if your parents had no children, chances are you wouldnt either <grin>

So you have to basically eliminate all assemblies, and whats left are parts.

You have to recurse down ther assembly tree, much like you would if using a DIR$ command, looking for files in multiple folders.
 
MacPT, Your solution worked great. Everything is much more clear now. It works fine.

This is the complete macro for anyone that wants it. Of course your plane names might be different. The edits that requires should be pretty easy.


On Error Resume Next

Dim swApp, Part, SelMgr, selObj As Object
Dim PrA2, selObjName, ObjString, AssyName, PrA, AssyName1, AssyName2 As String
Dim Fixed As Boolean
Dim ObjType As Long
Dim Children As Variant
Dim MyBool As Boolean

Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Set Part = swApp.ActiveDoc

AssyName1 = Part.GetTitle
AssyNameSize = Len(AssyName1)
AssyName2 = Mid(AssyName1, 1, AssyNameSize - 7)

' Get the selection manager class
Set SelMgr = Part.SelectionManager()
' Get the first item in the selection list
Set selObj = SelMgr.GetSelectedObject(1)

Children = selObj.GetChildren

'Count how many children in the object:
ChildCount = UBound(Children) 'ChildCount As Integer

'Finally test ChildCount:
If ChildCount = -1 Then 'you have a part.
PrA2 = &quot;P&quot;
Else
PrA2 = &quot;A&quot;
End If

' Get the name of the selected object
selObjName = selObj.Name2

Fixed = selObj.IsFixed
If Fixed = True Then
Part.UnfixComponent
End If

Part.ClearSelection

' Mate the Top Plane
PrA = PrA2
PrA = PrA + &quot;_TOP@&quot;
Part.Extension.SelectByID &quot;A_TOP&quot;, &quot;PLANE&quot;, 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + &quot;@&quot; + AssyName2
Part.Extension.SelectByID OjbString, &quot;PLANE&quot;, 0, 0, 0, True, 0, Nothing
Part.AddMate 0, 0, 0, 0, 0
Part.ClearSelection
If PrA2 = &quot;A&quot; Then
Part.Extension.SelectByID AssyName2, &quot;&quot;, 0, 0, 0, False, 0, Nothing
End If

' Mate the Front Plane
PrA = PrA2
PrA = PrA + &quot;_FRONT@&quot;
Part.Extension.SelectByID &quot;A_FRONT&quot;, &quot;PLANE&quot;, 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + &quot;@&quot; + AssyName2
Part.Extension.SelectByID OjbString, &quot;PLANE&quot;, 0, 0, 0, True, 0, Nothing
Part.AddMate 0, 0, 0, 0, 0
Part.ClearSelection
If PrA2 = &quot;A&quot; Then
Part.Extension.SelectByID AssyName2, &quot;&quot;, 0, 0, 0, False, 0, Nothing
End If

' Mate the Right Plane
PrA = PrA2
PrA = PrA + &quot;_RIGHT@&quot;
Part.Extension.SelectByID &quot;A_RIGHT&quot;, &quot;PLANE&quot;, 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + &quot;@&quot; + AssyName2
Part.Extension.SelectByID OjbString, &quot;PLANE&quot;, 0, 0, 0, True, 0, Nothing
Part.AddMate 0, 0, 0, 0, 0
Part.ClearSelection



Many thanks to all that responded. This will be a very handy macro for me.

Regards, Sean F
Mechanical Engineer
seanf@newing-halll.com
1 2 many l's in e-mail
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor