Copy UCS from part to assembly

Copy UCS from part to assembly

GeorgK
Advisor Advisor
1,687 Views
12 Replies
Message 1 of 13

Copy UCS from part to assembly

GeorgK
Advisor
Advisor

Hello together,

 

I have a part with a UCS inside. This part is placed in an assembly. I would like create a new UCS in the assembly with the same orientation as the part UCS.

 

 Dim oDoc As Document
        oDoc = m_invApp.ActiveDocument

        Dim CompDefs As AssemblyComponentDefinitions
        CompDefs = oDoc.ComponentDefinitions

        Dim oCompDef As AssemblyComponentDefinition
        oCompDef = CompDefs.Item(1)

        Dim oWx As Inventor.WorkAxis
        oWx = m_invApp.CommandManager.Pick(SelectionFilterEnum.kWorkAxisFilter, "Please select WorkAxis:")

        If oWx.IsCoordinateSystemElement Then

            Dim oWxNodeDef As Inventor.NativeBrowserNodeDefinition
            Dim oWxNode As Inventor.BrowserNode

            oWxNodeDef = oDoc.BrowserPanes.GetNativeBrowserNodeDefinition(oWx)
            oWxNode = oDoc.BrowserPanes.ActivePane.TopNode.AllReferencedNodes(oWxNodeDef).Item(1)

            Dim oUCS As Inventor.UserCoordinateSystem
            oUCS = oWxNode.Parent.NativeObject

            Dim oTransMatrix As Matrix
            oTransMatrix = oUCS.Transformation
          
            '.........................
        End If

 

How could I do this?

 

Thanks

 

Georg

0 Likes
Accepted solutions (2)
1,688 Views
12 Replies
Replies (12)
Message 2 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

In order to copy UCS from part to assembly, UserCoordinateSystemProxy can be created from part and copied into AssemblyDocument.

 

To illustrate the same,

 

  1. Launch Inventor 2018
  2. open new assembly document
  3. Download attached part(UCS part.ipt) to desired location
  4. Copy and paste the following VBA code into VBA editor.
  5. Update the path of part in VBA code.
  6. On running the code, a UCS will be copied from part to assembly.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition
    
    Dim occMatrix As Matrix
    Set occMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    Dim occ As ComponentOccurrence
    Set occ = oDef.Occurrences.Add("Path of part files\UCS part.ipt", occMatrix)
        
    Dim compDef As PartComponentDefinition
    Set compDef = occ.Definition
    
    Dim partUCS As UserCoordinateSystem
    Set partUCS = compDef.UserCoordinateSystems.Item(1)
      
    Dim oProxy As UserCoordinateSystemProxy
    Call occ.CreateGeometryProxy(partUCS, oProxy)
        
    Dim oMatrix As Matrix
    Set oMatrix = oProxy.Transformation
       
    Dim oUCSDef As UserCoordinateSystemDefinition
    Set oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
    
    oUCSDef.Transformation = oMatrix
    
    Dim oUCS As UserCoordinateSystem
    Set oUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
    
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

is it possible to select the UCS directly? I have several parts with different UCS in each part.

 

Thank you for your help.

 

Georg

 

 

0 Likes
Message 4 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

Try the following VBA code to select the UCS and create the same in AssemblyDocument.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition

    Dim oUCS As UserCoordinateSystem
    Set oUCS = ThisApplication.CommandManager.Pick(kUserCoordinateSystemFilter, "Select UCS")
    
    Dim oMatrix As Matrix
    Set oMatrix = oUCS.Transformation
       
    Dim oUCSDef As UserCoordinateSystemDefinition
    Set oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
    
    oUCSDef.Transformation = oMatrix
    
    Dim copiedUCS As UserCoordinateSystem
    Set copiedUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
    
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

your code does not position the UCS in the assembly at the position where the part with the UCS is. The part includes the UCS.

 

Thank you Georg

UCS.jpg

0 Likes
Message 6 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK

 

Try the following VBA code to copy UCS from part to assembly. Here, location of part occurrence plays important role in copying UCS. So, occurrence selection is required to copy UCS.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition

    Dim oUCS As UserCoordinateSystem
    Set oUCS = ThisApplication.CommandManager.Pick(kUserCoordinateSystemFilter, "Select UCS")
    
    Dim occ As ComponentOccurrence
    Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select occurrence from which UCS need to copy")
    
    Dim occMatrix As Matrix
    Set occMatrix = occ.Transformation
    
    Dim oMatrix As Matrix
    Set oMatrix = oUCS.Transformation
    Call oMatrix.TransformBy(occMatrix)
       
    Dim oUCSDef As UserCoordinateSystemDefinition
    Set oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
    
    oUCSDef.Transformation = oMatrix
    
    Dim copiedUCS As UserCoordinateSystem
    Set copiedUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
    
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

thank you very much. Now I understand the transformation. That's great. Is it possible to get the occurrence from the UCS by code?

 

Georg

0 Likes
Message 8 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @GeorgK,

 

Try the following VBA code which takes occurrence from UCS and copy UCS from part to assembly.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition

    Dim oUCS As UserCoordinateSystem
    Set oUCS = ThisApplication.CommandManager.Pick(kUserCoordinateSystemFilter, "Select UCS")
    
    Dim occDef As ComponentDefinition
    Set occDef = oUCS.Parent
    
    Dim occ As ComponentOccurrence
    Dim occMatrix As Matrix
    
    For Each occ In oDef.Occurrences
        If occ.Definition Is occDef Then
            Set occMatrix = occ.Transformation
        End If
    Next
    
    If occMatrix Is Nothing Then
        Debug.Print ("UCS occurrence not found")
    Else
     
        Dim oMatrix As Matrix
        Set oMatrix = oUCS.Transformation
        Call oMatrix.TransformBy(occMatrix)
           
        Dim oUCSDef As UserCoordinateSystemDefinition
        Set oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
        
        oUCSDef.Transformation = oMatrix
        
        Dim copiedUCS As UserCoordinateSystem
        Set copiedUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
        
    End If
    
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 9 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

thank you very much. That saves me a lot of time.

 

Georg

0 Likes
Message 10 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

is it possible to get the UCS from a part in a subassembly? How do I transforme the matrix?

 

Thank you

Georg

0 Likes
Message 11 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @GeorgK,

 

You could try the following VBA code. Here, only part occurrences are considered.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = oDoc.ComponentDefinition

    Dim oUCS As UserCoordinateSystem
    Set oUCS = ThisApplication.CommandManager.Pick(kUserCoordinateSystemFilter, "Select UCS")
    
    Dim occDef As ComponentDefinition
    Set occDef = oUCS.Parent
    
    Dim occ As ComponentOccurrence
    Dim occMatrix As Matrix
    
    For Each occ In oDef.Occurrences.AllLeafOccurrences
        If occ.Definition Is occDef Then
            Set occMatrix = occ.Transformation
            Exit For
        End If
    Next
    
    If occMatrix Is Nothing Then
        Debug.Print ("UCS occurrence not found")
    Else
     
        Dim oMatrix As Matrix
        Set oMatrix = oUCS.Transformation
        Call oMatrix.TransformBy(occMatrix)
           
        Dim oUCSDef As UserCoordinateSystemDefinition
        Set oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
        
        oUCSDef.Transformation = oMatrix
        
        Dim copiedUCS As UserCoordinateSystem
        Set copiedUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
        
    End If
    
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,

 

 

 

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 12 of 13

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

thank you very much. That solves the problem.

 

Georg

0 Likes
Message 13 of 13

bt_cn
Explorer
Explorer

Hello, is it also possible to copy the UCS from an assembly to a subassembly?

0 Likes