Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Macro or API to use "Invert Selection" to unsuppress components 2

Status
Not open for further replies.

rgrayclamps

Mechanical
Aug 6, 2004
376
I am trying to write a SW API or Macro to unsuppress all but selected components in an assembly. It seems that "Invert Selection" command is not available in SW API. I tried to record a macro, but it did not capture "Invert Selection" command. Does anyone know how to access SW "Invert Selection" command in api?

I tried a couple of "Unsuppress all but selected components" macro and api programs downloaded from the internet. But their performances are nowhere near that of native "Unsuppress" plus "Invert selection" commands in SolidWorks.

Thanks,

Alex
SW 2006 sp 3.4
VB 6.0
 
Replies continue below

Recommended for you

I found this bit of code as an example on how to invert selection within an assembly. I haven't tried it yet, but here it is, so give it a go. :)


Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swswSelMgr As SldWorks.SelectionMgr
Dim vComponents As Variant
Dim swSelComp As SldWorks.Component2
Dim selCount As Integer
Dim i As Long
Dim bretval As Boolean
Dim swSelObj

Sub main()

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc

If swModel.GetType = 2 Then

Set swconf = swModel.GetActiveConfiguration
Set swSelComp = swconf.GetRootComponent

vComponents = swSelComp.GetChildren

If UBound(vComponents) > 0 Then

Set swSelMgr = swModel.SelectionManager
selCount = swSelMgr.GetSelectedObjectCount

If selCount > 0 Then

ReDim swSelObj(selCount)

For i = 1 To selCount
If swSelMgr.GetSelectedObjectType2(i) = 20 Then
Set swSelObj(i) = swSelMgr.GetSelectedObject5(i)
Else
MsgBox "Please, select a component."
Exit Sub
End If
Next i

For i = 0 To UBound(vComponents)
bretval = vComponents(i).Select2(True, 0)
Next i

For i = 1 To selCount
swSelObj(i).DeSelect
Next i

End If
End If

Else
MsgBox "Please, open an Assembly."
End If

Set swModel = Nothing
Set swApp = Nothing

End Sub

 
Hi, fcsuper and flower72:

Thanks for your quick replies. It seems that the code does not work on an assembly with sub-assemblies.

I would prefer using "Invert Selection" if it is available thru. api.

Thanks,

Alex
 
If it were available in the API then nobody would go through the trouble of trying to re-create its function by writing a macro. You may be able to get around this by mapping the "Invert Selection" command to some hotkey combination and then using a SendKeys statement in your macro to send that key combination to SW. You may have to add a pause in your macro execution to allow SW to process the keystrokes prior to continuing. See thread766-151134 for the code to pause VBA execution.
 
Hi, Handleman:

That is a good advice. But how do I map the "Invert Selection" command as it is not in SW standard menu. The "Invert Selection" is accessed through a menu right clicked on screen area after selecting components from screen.

If I can create a hotkey combination for this command, I can send it to SW from VB. I do not need to pause for this as I preselect components which I want unsuppressed.

Thanks,

Alex
 
Invert Selection is a standard item in the Tools menu. The pause I mentioned is to allow SW to process the SendKeys statement before the VB continues execution. I have found that many times when I use SendKeys the code will continue to execute before the target application has finished processing the sent keystrokes, even when the "Wait" argument of the SendKeys statement is set to True.
 
Hi, Handleman:

Thanks! This works beautifully. I was unable to locate "Invert Selection" from SW menu.

By the way, you should not have to add any pause in VB when you send "SendKeys" statement. When you run "SendKeys" in VB, you just throw a series of commands into Windows's keyboard event queue to be executed. The "Wait" argument of the SendKeys state should take care of the problem.

There are times however that you may want to monitor execution of your statements to "slow down" your VB program to prevent overflow keyboard cache memory.

Alex
 
If you don't see "Invert Selection" in your Tools menu then go to Tools->Customize Menu and put a check-mark by it.

I agree that the Wait argument should take care of the problem. However, there have been times when I've used it that it didn't.
 
Care to share the results of your suggest, rgrayclamps? :)
 
Hi, fcsuper:

Would love to share everything I know.

What was my suggestion?

I just learnt from handleman that I can map my keyboard to "Invert Selection" command. So, here is what I do to suppress everything except the selected components/sub-assembies.

1. Preselect the components;
2. Send "Sendkeys" to SW using VB to execute "Invert Selection" (I assign "I" key to "Invert Selection" command.)
3. Send "Sendkeys" to SW using VB to execute "Suppress" command.

This method should also work in hide/show components. It is better than the couple of api programs I used before.

Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor