Open selected part of assembly in VBA

Open selected part of assembly in VBA

shastu
Advisor Advisor
4,198 Views
6 Replies
Message 1 of 7

Open selected part of assembly in VBA

shastu
Advisor
Advisor

If I have an assembly open and in the browser or from within the window have a part or assembly selected, what would be the VBA code to open the selected file and then close that file again?

0 Likes
Accepted solutions (1)
4,199 Views
6 Replies
Replies (6)
Message 2 of 7

MechMachineMan
Advisor
Advisor
Accepted solution
 Sub Main()  
Dim oDoc As Document Set oDoc = ThisApplication.ActiveDocument If oDoc Is Nothing Then Exit Sub End If Select Case True Case Not oDoc.ActivatedObject Is Nothing If oDoc.ActivatedObject.Type = ObjectTypeEnum.kDocumentObject Then Set oDoc = oDoc.ActivatedObject Else MsgBox ("Invalid active object") Exit Sub End If Case oDoc.ActivatedObject Is Nothing 'Continue without revising anything Case Else MsgBox ("Invalid Activated Object!") End Select Dim oSS As SelectSet Set oSS = oDoc.SelectSet Select Case True Case oSS.Count = 0 Call ToggleWP(oDoc) Case oSS.Count = 1 If oSS.Item(1).Type = ObjectTypeEnum.kDocumentObject Then MsgBox (oSS.Item(1).Type) Call ToggleWP(oSS.Item(1)) ElseIf oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceObject Or oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceProxyObject Then Call ToggleWP(oSS.Item(1).Definition.Document) Else MsgBox ("Invalid selected object!") End If Case oSS.Count > 1 For Each oItem In oSS If oItem.Type = ObjectTypeEnum.kDocumentObject Then ToggleWP (oItem) End If Next Case Else MsgBox ("Invalid Select Set!") End Select End Sub Sub ToggleWP(oDoc As Document) Set oNewDoc = ThisApplication.Documents.Open(oDoc.FullFileName, True) 'oNewDoc.Close End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 7

shastu
Advisor
Advisor

Fantastic.  I really wish I could figure this stuff out on my own instead of asking for help all the time.  I tried to make sense of using the Object Browser, but just can't get my head wrapped around it.  Thanks SO MUCH!!!

0 Likes
Message 4 of 7

MechMachineMan
Advisor
Advisor

 

The object model might be easier for you to understand where certain parentage of various functions/features exist.

 

http://help.autodesk.com.s3.amazonaws.com/sfdcarticles/kA23A000000PR5fSAG/Inventor2016ObjectModel.pd...

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 7

shastu
Advisor
Advisor

Thanks for your help,  I have seen the model before, but I don't understand how to use it.  For example,  I now have a program that opens the file that they had selected, saves it off as a different filename, updates some iproperties and then closes the files.  Now I want to replace the exact occurrence that they had selected with the new filename.  I have all the information passed from one sub to another, but how do I capture the exact occurrence that they had selected and reselect it.  Also how do I replace the component assuming I still have all file location and information still?  I assume I need to capture the selection set somehow, and then do a replace, but using the model, I don't know the exact nomenclature to make it work.

0 Likes
Message 6 of 7

shastu
Advisor
Advisor

Here is what I am needing.  Down at the bottom I have the FullFileName of the document that was selected and it will show up in the Msgbox oDoc.FullFileName.  The very next line I need to capture the occurrence name and then use that later to reselect that same occurrence.

 

Sub Main()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
   
    If oDoc Is Nothing Then
        Exit Sub
    End If
    
    Select Case True
        Case Not oDoc.ActivatedObject Is Nothing
            If oDoc.ActivatedObject.Type = ObjectTypeEnum.kDocumentObject Then
                Set oDoc = oDoc.ActivatedObject
            Else
                MsgBox ("Invalid active object")
                Exit Sub
            End If
        Case oDoc.ActivatedObject Is Nothing
            'Continue without revising anything
        Case Else
            MsgBox ("Invalid Activated Object!")
    End Select
   
    Dim oSS As SelectSet
    Set oSS = oDoc.SelectSet
   
    Select Case True
        Case oSS.Count = 0
            Call ToggleWP(oDoc)
        Case oSS.Count = 1
            If oSS.Item(1).Type = ObjectTypeEnum.kDocumentObject Then
                MsgBox (oSS.Item(1).Type)
                Call ToggleWP(oSS.Item(1))
            ElseIf oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceObject Or oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceProxyObject Then
               
                Call ToggleWP(oSS.Item(1).Definition.Document)
            Else
                MsgBox ("Invalid selected object!")
            End If
        Case oSS.Count > 1
            For Each oItem In oSS
                MsgBox "You should only select one item at a time before using the button just used"
                If oItem.Type = ObjectTypeEnum.kDocumentObject Then
                    ToggleWP (oItem)
                End If
            Next
        Case Else
            MsgBox ("Invalid Select Set!")
    End Select
End Sub

Sub ToggleWP(oDoc As Document)
Dim oCompOccur As Inventor.ComponentOccurrence
'Set oCompOccur = oCompOccur.name
MsgBox oDoc.FullFileName
MsgBox oCompOccur.name

End Sub

 

 

0 Likes
Message 7 of 7

MechMachineMan
Advisor
Advisor
 Sub Main()  
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    
    If oDoc Is Nothing Then
        Exit Sub
    End If
     
    Select Case True
        Case Not oDoc.ActivatedObject Is Nothing
            If oDoc.ActivatedObject.Type = ObjectTypeEnum.kDocumentObject Then
                Set oDoc = oDoc.ActivatedObject
            Else
                MsgBox ("Invalid active object")
                Exit Sub
            End If
        Case oDoc.ActivatedObject Is Nothing
            'Continue without revising anything
        Case Else
            MsgBox ("Invalid Activated Object!")
    End Select
    
    Dim oSS As SelectSet
    Set oSS = oDoc.SelectSet
    
    Select Case True
        Case oSS.Count = 0
            Exit Sub
        Case oSS.Count = 1
If oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceObject Or oSS.Item(1).Type = ObjectTypeEnum.kComponentOccurrenceProxyObject Then
                Call ToggleWP(oSS.Item(1))
            Else
                MsgBox ("Invalid selected object!")
            End If
        Case oSS.Count > 1
            For Each oItem In oSS
                If oItem.Type = ObjectTypeEnum.kComponentOccurrenceObject Or ObjectTypeEnum.kComponentOccurrenceProxyObject Then
                    ToggleWP (oItem)
                End If
            Next
        Case Else
            MsgBox ("Invalid Select Set!")
    End Select
End Sub

Sub ToggleWP(oOcc As ComponentOccurrence)
         Set oNewDoc = ThisApplication.Documents.Open(oOcc.Definition.Document.FullFileName, True)
         MsgBox(oOcc.Name)
         'oNewDoc.Close
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes