Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ilogic UCS constraint

4 REPLIES 4
Reply
Message 1 of 5
freudeind
1066 Views, 4 Replies

Ilogic UCS constraint

Hi,

I would like to constrain any parts in an assembly, that are not currently constrained, to an assembly UCS labeled UCS1.

 

I currently manually constrain parts (part UCS to assembly UCS) and it is extremely repetitive. Can anyone help me out with iLogic to check and see if a part is constrained, and if not it is not constrained, then constrain the part UCS to assmebly UCS.

 

Thanks,

Paul

4 REPLIES 4
Message 2 of 5
adam.nagy
in reply to: freudeind

Hi Paul,

 

I had a look and this is what I could come up with:

http://adndevblog.typepad.com/manufacturing/2014/07/create-ucs-constraints.html

 

Cheers,



Adam Nagy
Autodesk Platform Services
Message 3 of 5
freudeind
in reply to: adam.nagy

Hi Adam,

Can you please provide insight on how to apply the code? I tried to drop it into iLogic in Inventor 2015, but received errors (cannot compile rule).

Message 4 of 5
adam.nagy
in reply to: freudeind

Hi,

 

You just have to work through those errors.

1) First you'll get:

error1.png

 

So you rename CheckUcsConstraints function to Main and move that whole function to the beginning of the rule.

 

2) Then you'll get:

error2.png

So you remove all the Set keywords and when wpp is used for a second time in the code (inside CreateGeometryProxy), you declare a separate variable for that (e.g. wpp2)

 

And here is the end result that works:

Sub Main()
  ' Check if occurrences have a UCS1 and if it's constrained already
  Dim asm As AssemblyDocument
  asm = ThisApplication.ActiveDocument
    
  ' Using error handling in case
  ' not all components have a UCS1
  On Error Resume Next
  Dim asmUcs1 As UserCoordinateSystem
  asmUcs1 = _
    asm.ComponentDefinition.UserCoordinateSystems("UCS1")
    
  ' Each WorkPlane of the UCS must be constrained
  Call CreateFlushConstraints(asmUcs1.XYPlane, 0)
  Call CreateFlushConstraints(asmUcs1.XZPlane, 1)
  Call CreateFlushConstraints(asmUcs1.YZPlane, 2)
  On Error Goto 0
End Sub

Function GetAllOccurrences(ByVal cd As AssemblyComponentDefinition) As ObjectCollection
  Dim coll As ObjectCollection
  coll = ThisApplication.TransientObjects.CreateObjectCollection
    
  Dim occ As ComponentOccurrence
  For Each occ In cd.Occurrences
    Call coll.Add(occ)
  Next
    
  GetAllOccurrences = coll
End Function

Sub CreateFlushConstraints(wp As WorkPlane, plane As Integer)
  Dim acd As AssemblyComponentDefinition
  acd = wp.Parent

  Dim coll As ObjectCollection
  coll = GetAllOccurrences(acd)
    
  Dim obj As Object
  For Each obj In wp.Dependents
    If TypeOf obj Is FlushConstraint Then
      Dim f As FlushConstraint
      f = obj
            
      ' Get other entity
      Dim other As Object
      If f.EntityOne Is wp Then
        other = f.EntityTwo
      Else
        other = f.EntityOne
      End If
            
      ' If it's a WorkPlane proxy
      ' then it's from an occurrence
      If TypeOf other Is WorkPlaneProxy Then
        Dim wpp As WorkPlaneProxy
        wpp = other
                
        Call coll.RemoveByObject(wpp.ContainingOccurrence)
      End If
    End If
  Next
    
  ' Create Flush Constraint for the remaining occurrences
  Dim occ As ComponentOccurrence
  For Each occ In coll
    Dim ucs As UserCoordinateSystem
    ucs = occ.Definition.UserCoordinateSystems("UCS1")
        
    Dim occWp As WorkPlane
    Select Case plane
      Case 0
        occWp = ucs.XYPlane
      Case 1
        occWp = ucs.XZPlane
      Case 2
        occWp = ucs.YZPlane
    End Select
      
    Dim wpp2 As WorkPlaneProxy	
    Call occ.CreateGeometryProxy(occWp, wpp2)
    Call acd.Constraints.AddFlushConstraint(wp, wpp2, 0)
  Next
End Sub

 Cheers,



Adam Nagy
Autodesk Platform Services
Message 5 of 5
freudeind
in reply to: adam.nagy

Excellent! Thanks Adam.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report