Create origin flush constraints

Create origin flush constraints

MikeOG
Contributor Contributor
1,521 Views
8 Replies
Message 1 of 9

Create origin flush constraints

MikeOG
Contributor
Contributor

Hi, can someone please help me with some ilogic. I want a rule that that will create origin flush constraint for any / all parts that I run it on at assembly level. It must have an option to turn Ground at origin on or off in the code. There is a toolbar for this under the productivity tab (Assemble Ribbon), but I would like to map it to a rule / keyboard shortcut.

 

MikeOG_0-1693338495521.png

 

I found some code that does only the grounded bit, still need it to "Create origin flush constraints".

 

Sub Main
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument

    Dim oOcc As ComponentOccurrence
    oOcc = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "SelectPart")
	
	oOcc.Grounded = False
	For Each oConstraint As AssemblyConstraint In oOcc.Constraints
		oConstraint.Delete
	Next
	oOcc.Transformation = ThisApplication.TransientGeometry.CreateMatrix
	oOcc.Grounded = True
End Sub

 

 

0 Likes
Accepted solutions (1)
1,522 Views
8 Replies
Replies (8)
Message 2 of 9

Frederick_Law
Mentor
Mentor

I did constraint to 3 planes, in VB.NET addin:

    Public Sub Button1_Execute()
      Dim oDoc As Document
      Dim oSelectSet As SelectSet
      Dim AssmYZPlane As WorkPlane
      Dim AssmXZPlane As WorkPlane
      Dim AssmXYPlane As WorkPlane
      'Dim CompYZPlane As WorkPlane
      'Dim CompXZPlane As WorkPlane
      'Dim CompXYPlane As WorkPlane
      Dim ProxyYZPlane As WorkPlaneProxy
      Dim ProxyXZPlane As WorkPlaneProxy
      Dim ProxyXYPlane As WorkPlaneProxy
      Dim oFlush As FlushConstraint
      Dim oComponent As ComponentOccurrence
      Dim oAsmCompDef As AssemblyComponentDefinition
      Dim oTxnMgr As TransactionManager
      Dim oTxn As Transaction

      Try
        oDoc = AddinGlobal.IVApp.ActiveDocument
        If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
          oSelectSet = AddinGlobal.IVApp.ActiveDocument.SelectSet
          If oSelectSet.Count > 0 Then
            oAsmCompDef = AddinGlobal.IVApp.ActiveDocument.ComponentDefinition
            AssmYZPlane = oDoc.ComponentDefinition.WorkPlanes.Item("YZ Plane")
            AssmXZPlane = oDoc.ComponentDefinition.WorkPlanes.Item("XZ Plane")
            AssmXYPlane = oDoc.ComponentDefinition.WorkPlanes.Item("XY Plane")

            oTxnMgr = AddinGlobal.IVApp.TransactionManager
            oTxn = oTxnMgr.StartTransaction(oDoc, "Mate to Origin")

            For Each oComponent In oSelectSet
              '      Set CompYZPlane = oComponent.Definition.WorkPlanes.Item("YZ Plane")
              Call oComponent.CreateGeometryProxy(oComponent.Definition.WorkPlanes.Item("YZ Plane"), ProxyYZPlane)

              '      Set CompXZPlane = oComponent.Definition.WorkPlanes.Item("XZ Plane")
              Call oComponent.CreateGeometryProxy(oComponent.Definition.WorkPlanes.Item("XZ Plane"), ProxyXZPlane)

              '      Set CompXYPlane = oComponent.Definition.WorkPlanes.Item("XY Plane")
              Call oComponent.CreateGeometryProxy(oComponent.Definition.WorkPlanes.Item("XY Plane"), ProxyXYPlane)

              oFlush = oAsmCompDef.Constraints.AddFlushConstraint(ProxyYZPlane, AssmYZPlane, 0)
              oFlush = oAsmCompDef.Constraints.AddFlushConstraint(ProxyXZPlane, AssmXZPlane, 0)
              oFlush = oAsmCompDef.Constraints.AddFlushConstraint(ProxyXYPlane, AssmXYPlane, 0)
            Next
            oTxn.End()
          Else
            MsgBox("Select parts to mate.")
          End If
        End If


      Catch ex As Exception
        Windows.Forms.MessageBox.Show(ex.ToString())
      End Try
    End Sub

 

Should be able to get "Center Point" from WorkPoints:

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-155178B3-E035-411C-BEB9-E0FA9D1D4462

0 Likes
Message 3 of 9

MikeOG
Contributor
Contributor

If possible, can someone write an ilogic code for this please?

0 Likes
Message 4 of 9

Frederick_Law
Mentor
Mentor

iLogic is based on VB.

You'll need to fix 20%.

0 Likes
Message 5 of 9

MikeOG
Contributor
Contributor

My coding skills are unfortunately very limited hence me asking for someone to write an ilogic rule that I can just plug straight in.

0 Likes
Message 6 of 9

Frederick_Law
Mentor
Mentor

While you're waiting for someone to do it for you, you could try and see how far you can get and learn something.

Unless you're too busy for that too 😉

0 Likes
Message 7 of 9

MikeOG
Contributor
Contributor

@Frederick_Law I usually give it a go first, try and piece some snippets together and trial and error till I get it to work, just too busy atm, and if someone has already written something that could work it would save me some time.. Thank you for the VB code though, I do appreciate it. 😁👍

0 Likes
Message 8 of 9

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @MikeOG . This is the macro I use in my work. Before running the macro, you need to select the components on which you want to set Flush constraints.

Private Sub Main()
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = oInvApp.ActiveDocument
	Dim oTM As TransactionManager = oInvApp.TransactionManager
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oAssDoc As AssemblyDocument = oDoc
		Dim oSelSet As SelectSet = oAssDoc.SelectSet
		If oSelSet.Count < 1 Then
			MessageBox.Show("It is necessary to select at least one component!", "Error!", _
							MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End If
		Dim sListOccs As New List(Of ComponentOccurrence)
		For i As Integer = 1 To oSelSet.Count
			If TypeOf oSelSet.Item(i) Is ComponentOccurrence Then
				sListOccs.Add(oSelSet.Item(i))
			End If
		Next i
		Dim newTM As Transaction = oTM.StartTransaction(oAssDoc, "Constraints_XYZ")
		For iOcc As Integer = 0 To sListOccs.Count - 1
			Call CreatFlushConstraints(oAssDoc.ComponentDefinition, sListOccs(iOcc))
		Next
		newTM.End
	Else
		MessageBox.Show("This rule is intended for work with assemblies!", "Error!", _
						MessageBoxButtons.OK, MessageBoxIcon.Error)
	End If 
End Sub

Private Function CreatFlushConstraints(ByVal oAssDef As AssemblyComponentDefinition,
										ByVal oOcc As ComponentOccurrence)
	Dim oAsmPlane As WorkPlane
	Dim oPartPlane As WorkPlane
	Dim oPartPlaneProxy As WorkPlaneProxy
	For i As Integer = 1 To 3
		oAsmPlane = oAssDef.WorkPlanes(i)
		oPartPlane = oOcc.Definition.WorkPlanes(i)
		Call oOcc.CreateGeometryProxy(oPartPlane, oPartPlaneProxy)
		Call oAssDef.Constraints.AddFlushConstraint(oPartPlaneProxy, oAsmPlane, 0)
	Next i
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 9 of 9

MikeOG
Contributor
Contributor

@Andrii_Humeniuk - Perfect! Thank you!

0 Likes