×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

API Assembly selection

API Assembly selection

API Assembly selection

(OP)
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

RE: API Assembly selection

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.

RE: API Assembly selection

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.

RE: API Assembly selection

>> 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.

RE: API Assembly selection

(OP)
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("SldWorks.Application")
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 = "P"
    Else
        PrA2 = "A"
    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 + "_TOP@"
Part.Extension.SelectByID "A_TOP", "PLANE", 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + "@" + AssyName2
Part.Extension.SelectByID OjbString, "PLANE", 0, 0, 0, True, 0, Nothing
Part.AddMate 0, 0, 0, 0, 0
Part.ClearSelection
    If PrA2 = "A" Then
        Part.Extension.SelectByID AssyName2, "", 0, 0, 0, False, 0, Nothing
    End If
    
' Mate the Front Plane
PrA = PrA2
PrA = PrA + "_FRONT@"
Part.Extension.SelectByID "A_FRONT", "PLANE", 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + "@" + AssyName2
Part.Extension.SelectByID OjbString, "PLANE", 0, 0, 0, True, 0, Nothing
Part.AddMate 0, 0, 0, 0, 0
Part.ClearSelection
    If PrA2 = "A" Then
        Part.Extension.SelectByID AssyName2, "", 0, 0, 0, False, 0, Nothing
    End If
    
' Mate the Right Plane
PrA = PrA2
PrA = PrA + "_RIGHT@"
Part.Extension.SelectByID "A_RIGHT", "PLANE", 0, 0, 0, False, 0, Nothing
OjbString = PrA + selObjName + "@" + AssyName2
Part.Extension.SelectByID OjbString, "PLANE", 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources