Inventor Customization

Reply
Valued Contributor
michael.marx
Posts: 125
Registered: ‎04-20-2006
Message 1 of 5 (292 Views)
Accepted Solution

isolate an occurrence

292 Views, 4 Replies
08-13-2012 07:17 AM

Hi

 

i don´t find a way to isolate and undo isolate an occurrence in Assembly can anybody help me wih the right code in VB.

 

regards Michael

two issues here:

 

1) the occurrence name contains ‘ : ‘, which is not accepted as a char in file name. This is Windows rule. So e.g.

myPart:1.stp is truncated as myPart.

That is why you saw the file name without extension name. And the file size is 0

 

2) ControlDefiniton.Execute is Post Command (asynchronously). That means it will be executed after the function which invokes it. So to Send Command (Synchronous), you need to use Execute2(true)

Please refer to the code below. note: you will need to write a method youeself to filter the invalid chars before saving.

 

    Private Sub test( )
 
          Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
 
        Dim _invApp As Inventor.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        
        If _invApp.Documents.Count = 0 Then
            MsgBox("Need to open an Assembly document")
            Return
        End If
 
        If _invApp.ActiveDocument.DocumentType <> _
            DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("Need to have an Assembly document active")
            Return
        End If
 
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument
        If asmDoc.SelectSet.Count = 0 Then
            MsgBox("Need to select a Part or Sub Assembly")
            Return
        End If
 
        Dim selSet As SelectSet
        selSet = asmDoc.SelectSet
 
        Dim compOcc As ComponentOccurrence 
        For Each obj In selSet
 
            If TypeOf obj is ComponentOccurrence then            
            compOcc = obj
            If TypeOf compOcc.Definition.Document Is PartDocument Then
                Dim oCtrlDef As ControlDefinition
                'isolate
                oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateCmd")
                oCtrlDef.Execute2(true)
 
                Dim oDoc As Document
                ' it is still assembly context. So active document is still the assembly
                'oDoc = _invApp.ActiveDocument
 
                ' get the corresponding part document.
                oDoc = compOcc.Definition.Document
 
                Dim oFilename As String
                ' because ': ' is not accepted, 
                ' you need to write a function to filter the char
 
                ' for simple test, i use the part file name
 
                'oFilename = compOcc.Name & ".stp"
                
                oFilename = oDoc.DisplayName &  ".stp"
                Dim oFilepath As String
                oFilepath = "c:\temp\"
                Dim oPathName As String
                oPathName = oFilepath & oFilename
                'export step
                Call oDoc.SaveAs(oPathName, True)
                'undo isolate
                oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateUndoCmd")
                oCtrlDef.Execute2(true)
            End If
          End If
        Next
 
    End Sub

 

ADN Support Specialist
xiaodong.liang
Posts: 1,273
Registered: ‎06-12-2011
Message 2 of 5 (277 Views)

Re: isolate an occurrence

08-16-2012 11:36 PM in reply to: michael.marx

hi,

 

I did not fine a direct property to toggle [isolate] or [undo isolate]. But you could select the occurrence and execute the commands .e.g

 

Sub isolate_undoisolate()

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oOcc As ComponentOccurrence
Set oOcc = oDoc.ComponentDefinition.Occurrences.Item(1)

oDoc.SelectSet.Clear
oDoc.SelectSet.Select oOcc

Dim oCtrlDef As ControlDefinition

'isolate
Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyIsolateCmd")


'undo isolate
'Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyIsolateUndoCmd")


oCtrlDef.Execute
oDoc.SelectSet.Clear

End Sub



Xiaodong Liang
Developer Technical Services
Autodesk Developer Network

Valued Contributor
michael.marx
Posts: 125
Registered: ‎04-20-2006
Message 3 of 5 (261 Views)

Re: isolate an occurrence

08-21-2012 02:07 AM in reply to: michael.marx

hi

 

Thank you for your reply, but no i have another issue.

Problem description i want to iterate through a select set of occurrences and each occurrence should isolated saved as stp from within the the top assembly with occurrence name.

first problem is the occurence name. with compocc.name & ".stp"  i think i get the name with stp file extension but something went wrong i get a file with the right name but no extension?!?

second problem is the iteration through the select set but i don´t know why.

 

Sorry for my miserable programming but i´m a newbie to this :-)

 

Dim _invApp As Inventor.Application

If _invApp.Documents.Count = 0 Then
MsgBox("Need to open an Assembly document")
Return
End If
If _invApp.ActiveDocument.DocumentType <> _
DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("Need to have an Assembly document active")
Return
End If
Dim asmDoc As AssemblyDocument
asmDoc = _invApp.ActiveDocument
If asmDoc.SelectSet.Count = 0 Then
MsgBox("Need to select a Part or Sub Assembly")
Return
End If
Dim selSet As SelectSet
selSet = asmDoc.SelectSet

 

For Each obj In selSet
compOcc = obj
If TypeOf compOcc.Definition.Document Is PartDocument Then

Dim oCtrlDef As ControlDefinition

'isolate
oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateCmd")
oCtrlDef.Execute()

Dim oDoc As Document
oDoc = _invApp.ActiveDocument
Dim oFilename As String
oFilename = compOcc.Name & ".stp"
Dim oFilepath As String
oFilepath = "d:\temp\"
Dim oPathName As String
oPathName = oFilepath & oFilename

'export step
Call oDoc.SaveAs(oPathName, True)

'undo isolate
oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateUndoCmd")
oCtrlDef.Execute()


End If

Next

ADN Support Specialist
xiaodong.liang
Posts: 1,273
Registered: ‎06-12-2011
Message 4 of 5 (235 Views)

Re: isolate an occurrence

08-23-2012 08:45 PM in reply to: michael.marx

two issues here:

 

1) the occurrence name contains ‘ : ‘, which is not accepted as a char in file name. This is Windows rule. So e.g.

myPart:1.stp is truncated as myPart.

That is why you saw the file name without extension name. And the file size is 0

 

2) ControlDefiniton.Execute is Post Command (asynchronously). That means it will be executed after the function which invokes it. So to Send Command (Synchronous), you need to use Execute2(true)

Please refer to the code below. note: you will need to write a method youeself to filter the invalid chars before saving.

 

    Private Sub test( )
 
          Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
 
        Dim _invApp As Inventor.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        
        If _invApp.Documents.Count = 0 Then
            MsgBox("Need to open an Assembly document")
            Return
        End If
 
        If _invApp.ActiveDocument.DocumentType <> _
            DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("Need to have an Assembly document active")
            Return
        End If
 
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument
        If asmDoc.SelectSet.Count = 0 Then
            MsgBox("Need to select a Part or Sub Assembly")
            Return
        End If
 
        Dim selSet As SelectSet
        selSet = asmDoc.SelectSet
 
        Dim compOcc As ComponentOccurrence 
        For Each obj In selSet
 
            If TypeOf obj is ComponentOccurrence then            
            compOcc = obj
            If TypeOf compOcc.Definition.Document Is PartDocument Then
                Dim oCtrlDef As ControlDefinition
                'isolate
                oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateCmd")
                oCtrlDef.Execute2(true)
 
                Dim oDoc As Document
                ' it is still assembly context. So active document is still the assembly
                'oDoc = _invApp.ActiveDocument
 
                ' get the corresponding part document.
                oDoc = compOcc.Definition.Document
 
                Dim oFilename As String
                ' because ': ' is not accepted, 
                ' you need to write a function to filter the char
 
                ' for simple test, i use the part file name
 
                'oFilename = compOcc.Name & ".stp"
                
                oFilename = oDoc.DisplayName &  ".stp"
                Dim oFilepath As String
                oFilepath = "c:\temp\"
                Dim oPathName As String
                oPathName = oFilepath & oFilename
                'export step
                Call oDoc.SaveAs(oPathName, True)
                'undo isolate
                oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateUndoCmd")
                oCtrlDef.Execute2(true)
            End If
          End If
        Next
 
    End Sub

 



Xiaodong Liang
Developer Technical Services
Autodesk Developer Network

Valued Contributor
michael.marx
Posts: 125
Registered: ‎04-20-2006
Message 5 of 5 (230 Views)

Re: isolate an occurrence

08-23-2012 11:02 PM in reply to: michael.marx

Hi,

 

thats exactly i want to do!

Thank you for your help.

 

Michael

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.