Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic Insert Constraint

14 REPLIES 14
Reply
Message 1 of 15
Blokkie862
3844 Views, 14 Replies

iLogic Insert Constraint

Hello,

 

I want to automatically add a flush/mate constraint in my assembly between 2 parts between two specified surfaces. I have found this piece of code that inserts a constraint between 2 parts, but I have no idea how I can make it use the faces I want it to. Right now this piece of code picks two surfaces and adds a constraint. I don't know how I can tell iLogic that I want Part A, surface(...) to connect with Part B, surface(...), or how I can define the name of the surface I need.

 

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

Dim colCons As AssemblyConstraints
colCons = oDoc.ComponentDefinition.Constraints

Dim oOcc1 As ComponentOccurrence
Dim oOcc2 As ComponentOccurrence

oOcc1 = oDoc.ComponentDefinition.Occurrences(1)
oOcc2 = oDoc.ComponentDefinition.Occurrences(2)

Dim oEntity1 As Object
Dim oEntity2 As Object

oEntity1 = oOcc1.SurfaceBodies(1).Faces(1)
oEntity2 = oOcc2.SurfaceBodies(1).Faces(2)

colCons.AddFlushConstraint(oEntity1, oEntity2, 0)

Thanks for the help,

Robbert
14 REPLIES 14
Message 2 of 15
MingweiGao
in reply to: Blokkie862

Hello Robbert,

 

I think you should pre-define some attributes to tell iLogic what surfaces will be used for the mate.

 

Below is a simple example by using iMate to pre-define the attributes of the surfaces – just define an iMate on the surfaces.

 

 

Dim oAssyDoc As AssemblyDocument

oAssyDoc = ThisApplication.ActiveDocument

 

Dim oOcc1 As ComponentOccurrence

Dim oOcc2 As ComponentOccurrence

 

oOcc1 = oAssyDoc.ComponentDefinition.Occurrences(1)

oOcc2 = oAssyDoc.ComponentDefinition.Occurrences(2)

 

Dim oPartDef1 As PartComponentDefinition

oPartDef1 = oOcc1.Definition

 

Dim oPartDef2 As PartComponentDefinition

oPartDef2 = oOcc2.Definition

 

Dim iMate1 As iMateDefinition

iMate1 = oPartDef1.iMateDefinitions.Item(1)

 

Dim iMate2 As iMateDefinition

iMate2 = oPartDef2.iMateDefinitions.Item(1)

 

Dim oEntity1 As Face

Dim oEntity2 As Face

 

Call oOcc1.CreateGeometryProxy(iMate1.Entity, oEntity1)

Call oOcc2.CreateGeometryProxy(iMate2.Entity, oEntity2)

 

 

Dim colCons As AssemblyConstraint

colCons = oAssyDoc.ComponentDefinition.Constraints.AddMateConstraint(oEntity1, oEntity2, 0)

 



Steven Gao

Sr. SQA Engineer

Message 3 of 15
Blokkie862
in reply to: MingweiGao

Hey Mingwei,

 

Thanks for helping me. When I run the code you provided I get the error:

 

Error in rule: Constraint, in document: Rollenbaan Skeletal Assem 3.iam

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

More Info: 

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.iMateDefinitions.get_Item(Int32 Index)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

I have attached my assembly with the post. My goal is to add a mate constraint between the downside of "BaanFrame 3:1" and the top of "Poot 3:1". The part "Poot 3" is placed multiple times with iLogic, all of them need to be constraint when placed. I also want the part "Poot 3" to have a flush constraint at the side of "BaanFrame 3:1" so that it can only slide along the x axis when both constraints are in place.

 

When there are 2 different constraints to be placed, will this tactic with iMates still work?

 

Thanks in advance,

 

Robbert 

 

Message 4 of 15
tsreagan
in reply to: Blokkie862

You are most likely getting that error because iLogic is looking for something that does not exits. Probalby looking for your custom imate where none exists.  You may need error checking to safetly skip parts that do not have the custom imates.

 

T.S.

Message 5 of 15
Blokkie862
in reply to: tsreagan

Hello,

 

I have used the line On Error Goto, it showed that the point where the error pops up is in the line:

 

Dim iMate1 As iMateDefinition
iMate1 = oPartDef1.iMateDefinitions.Item(1)

 

I don't know in witch part the code is looking for the iMate, or how I can define in witch part it should look. Do you know how to solve my problem?

 

Thanks

Message 6 of 15
tsreagan
in reply to: Blokkie862

Ok,  Looking back at your original request, we need some more detail.

 

1.  Do you want to place 2 parts in your assebmly, and pick two faces, and have this rule create a constraint?

 

2. Do you want to place 2 parts in your assembly, run a rule, and have it automatically constrain two particular faces with a specific imate name/names?

 

3. Do you want to have a rule that, when ran, will cycle through all of your parts and create constraints between two faces that have an imate of a particular name?

 

The first is easy.

 

The second will require you to have pre-created the imates on faces in the parts.

 

The thrid will be a real pain,  you have to have pre-created imates, and you will either need well controlled imate naming, or some way to guide iLogic as to what part should be constrained to what other part,  this could be done many ways,  part proximity to another part, etc...

 

T.S.

 

Message 7 of 15
Blokkie862
in reply to: tsreagan

It is a combination of option 2 and 3. 

 

Currently I am placing a row of support legs ("Poot 3") along a frame ("BaanFrame") with an iLogic rule, their location depends on the length of the frame. The placing is done within a while loop. So I thought, maybe when one supportleg is placed, that specific supportleg can be constrained to the frame, and then goes on to placing the next leg.

 

The two faces that need to be constrained to each other are always the same, only the loction where along x-axis on the face of "BaanFrame 3" varies from leg to leg. I dont know if I need or want to use iMates, as long as the legs are constrained to the frame my problem is solved 🙂 

 

Thanks

Message 8 of 15
tsreagan
in reply to: Blokkie862

Ok,

 

Some assumptions:

 

Your current rule places your parts in the correct place, only without proper constraints being created?

 

If this is the case,  your original code could be modified to find the flush faces between the inserted part and the base, and create a constraint between the two.    Squeezing this in while you create/place the components with iLogic may save code and complexity. This method would not need imates,  Is this what you want to achieve?

 

T.S.

Message 9 of 15
Blokkie862
in reply to: tsreagan

Yes, that is exactly what I want to achieve, sorry if I haven't been very clear.

 

The parts are placed at the right location one by one with a while loop. The number of parts beeing placed depends on the lenght of the frame, the longer the frame, more support legs are placed. The two faces are already touching each other, but they arent constrained yet. I'm glad to hear this is possible.

 

I attached a few screenshots, maybe this makes it more clear. In screenshot 1 shows the two parts that need to be constrained to each other, screenshot 2 shows the faces that need to be mated (Face 2 is at the downside of the frame), and screenshot 3 shows the allignment of the supportlegs after I have run my rule for placement.

 

Thanks!

Message 10 of 15
tsreagan
in reply to: Blokkie862

Definately possible,  I don't know the exact syntax,  but the logic I can help with.

 

Is it possible to post the portion of your iLogic that is creating the components?

Then we can then see where/when we need to define faces and where we can add the constraint routines.

 

Initially you will have to find and define the face on the base component. (this will most likely be related or taken from your placement routine in some way)

Then as you work through the placement the loop, it will have to narrow down the desired face from each leg, and create the constraint between the two, then continue placing and constraining untill the loop is complete.

 

 

If your upgrading to 2014, there is a new constraint type that you may want to explore instead, at least if these parts are not meant to move in relation to each other, it is a new lock feature, that allows one part to lock in relation to another part, then you will be constrained in all directions at once,  and the legs will only move if the entire base moves.  without this you are only adding a constraint in one direction,  unless you wish to use this iLogic to create multiple constraints in various directions after tackling this.

 

T.T.

Message 11 of 15
MingweiGao
in reply to: Blokkie862

Attach my test dataset in here.



Steven Gao

Sr. SQA Engineer

Message 12 of 15
Blokkie862
in reply to: tsreagan

Hello,

 

I'm running Inventor 2013 now and I will propebly stick with it for a while, and exactly what you said, after I know how this is done I can use it for other cases aswel.

 

This is a piece of the code that I use to place the legs:

 

oPath = ThisDoc.Path & "\"
oFile = "Poot 3.ipt"

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
    
Dim oMatrix As Matrix
oMatrix = oTG.CreateMatrix

Dim oOccurrence As ComponentOccurrence

Else If BL >= 1500 Then

AP = (Round( BL/750)-1)
PA = -((BL-50)/AP)/10
PA2 = PA

Teller2 = 1

If EerstePoot = True Then
oMatrix.SetTranslation(oTG.CreateVector(-2.5, 0, -35.3)) 
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) 
'oOccurrence.Grounded = TrueComponent.Color(oOccurrence.Name) = "Red"
End If

While Teller2 <= AP

oMatrix.SetTranslation(oTG.CreateVector((PA-2.5), 0, -35.3)) 
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) 
'oOccurrence.Grounded = TrueComponent.Color(oOccurrence.Name) = "Red"

PA = PA + PA2
Teller2 = Teller2 +1

End While

End If

 

These are pieces of the code that I use to place to support legs. 

oMatrix.SetTranslation(oTG.CreateVector((PA-2.5), 0, -35.3)) 
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix) 

This is the line where the leg gets placed. The parameters PA, PA2 an AP are derived from user parameters and Teller is a counter for my loop. By the way, I have uploaded my assembly a few posts back, the code is within this assembly if you would like to see it. 

 

I have opened youre assembly with the Imate's, works just like I need it to, but when I want to place more and different constraints will that still work?

 

Thanks for your'e time and effort,

 

Robbert

 

Message 13 of 15
Carthik_Babu
in reply to: Blokkie862

I am unable to open your file,
check out my projects based on ilogic. hope it helps in your project
http://grabcad.com/carthik-1/projects
Carthik Babu M.S, Asst Manager - Machine Building,
Gabriel India Ltd,Hosur, TN, INDIA
Email:carthik_ms@yahoo.co.in ,
https://grabcad.com/carthik-1/projects
"May all beings be happy" http://www.dhamma.org/
Message 14 of 15
tsreagan
in reply to: MingweiGao

I was working with him 1 on 1 for a bit,  he may have this resolved.

 

T.S.

Message 15 of 15
Blokkie862
in reply to: tsreagan

I have been working on other things lately, but I'm back on FDS at the moment. Here is the code that T.S had made for me so far:

 

Parameter.UpdateAfterChange = True

BL = BaanLengte2
PA = 0
AP = 0

oPath = ThisDoc.Path & "\"
oFile = "Poot 3.ipt"


Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

Dim oFaces As ObjectCollection
oFaces = ThisApplication.TransientObjects.CreateObjectCollection
Dim o2Faces As ObjectCollection
o2Faces = ThisApplication.TransientObjects.CreateObjectCollection
Dim o3Faces As ObjectCollection
o3Faces = ThisApplication.TransientObjects.CreateObjectCollection

Dim oMatrix As Matrix
oMatrix = oTG.CreateMatrix

Dim oOccurrence As ComponentOccurrence

Try

If Teller2 = Teller3 Or Teller2 >= 1 Then
i = 1
AP = Round( BL/350)
While i < (AP+100)

doc = ThisDoc.Document
Dim compOcc As ComponentOccurrence
compOcc = Component.InventorComponent("Poot 3:"& Teller3)
compOcc.Delete()
i = i+1
Teller3 = Teller3+1
End While
Teller2 = 0

End If
Catch ex As Exception
Teller3 = 1


End Try

If BL < 1500 Then
PA = -(BL - 50)/10
Teller2 = 1
If EerstePoot = True Then
oMatrix.SetTranslation(oTG.CreateVector(-2.5, 0, -35.3))
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix)
'oOccurrence.Grounded = TrueComponent.Color(oOccurrence.Name) = "Red"
Teller2 = i + 1
End If

oMatrix.SetTranslation(oTG.CreateVector((PA-2.5), 0, -35.3))
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix)
'oOccurrence.Grounded = TrueComponent.Color(oOccurrence.Name) = "Red"
Teller2 = i + 1

Else If BL >= 1500 Then

AP = (Round( BL/750)-1)
PA = -((BL-50)/AP)/10
PA2 = PA

'MessageBox.Show("Aantalpoten="& AP & " PootAfstand="& PA &" PA2="& PA2)
Teller2 = 1

If EerstePoot = True Then
oMatrix.SetTranslation(oTG.CreateVector(-2.5, 0, -35.3))
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix)
'oOccurrence.Grounded = TrueComponent.Color(oOccurrence.Name) = "Yellow"

'Alle faces van de eerste poot worden hier verzameld en gefilterd
Dim oPartCompDef As PartComponentDefinition
oPartCompDef = oOccurrence.Definition
Dim oExtrudeFeature As ExtrudeFeature
oExtrudeFeature = oPartCompDef.Features.ExtrudeFeatures.Item(1)
Dim oExtrudeFeatureProxy As ExtrudeFeatureProxy
oOccurrence.CreateGeometryProxy (oExtrudeFeature, oExtrudeFeatureProxy)
Dim oFaceProxy As FaceProxy
    
    'Filteren naar het bovenste face van de eerste poot voor de mate constraint    For Each oFaceProxy In oExtrudeFeatureProxy.Faces
        Dim oFPoint As Point
        oFPoint = oFaceProxy.PointOnFace()
        If oFPoint.Z > -6 Then
            'MsgBox(oFPoint.X &",Y"&  oFPoint.Y & " Z" & oFPoint.Z)            oFaces.Add(oFaceProxy)
        End If
        Next
    
    'Filteren naar de rechter zijkant face van de eerste poot voor de flush constraint            Dim o3FaceProxy As FaceProxy        
    For Each o3FaceProxy In oExtrudeFeatureProxy.Faces 
        Dim oFPoint As Point
        oFPoint = o3FaceProxy.PointOnFace()
        If oFPoint.Y > 24 Then
        'MsgBox(oFPoint.X &",Y"&  oFPoint.Y & " Z" & oFPoint.Z)        o3Faces.Add(o3FaceProxy)
        End If
        Next

End If

While Teller2 <= AP

'MessageBox.Show("Aantalpoten="& AP & " PootAfstand="& PA &" PA2="& PA2)
oMatrix.SetTranslation(oTG.CreateVector((PA-2.5), 0, -35.3))
oOccurrence = oAsmCompDef.Occurrences.Add(oPath & oFile, oMatrix)
oOccurrence.Grounded = True
Component.Color(oOccurrence.Name) = "Red"

PA = PA + PA2
Teller2 = Teller2 +1

'Alle faces van de te plaatsen poten worden verzameldDim oPartCompDef As PartComponentDefinition
oPartCompDef = oOccurrence.Definition
Dim oExtrudeFeature As ExtrudeFeature
oExtrudeFeature = oPartCompDef.Features.ExtrudeFeatures.Item(1)
Dim oExtrudeFeatureProxy As ExtrudeFeatureProxy
oOccurrence.CreateGeometryProxy (oExtrudeFeature, oExtrudeFeatureProxy)
Dim oFaceProxy As FaceProxy

'Filteren naar het bovenste face van de poot voor de mate constraint    For Each oFaceProxy In oExtrudeFeatureProxy.Faces
        Dim oFPoint As Point
        oFPoint = oFaceProxy.PointOnFace()
        If oFPoint.Z > -6 Then
            'MsgBox(oFPoint.X &","&  oFPoint.Y & " " & oFPoint.Z)            oFaces.Add(oFaceProxy)
        End If
       Next

'Filteren naar de rechter zijkant face van de poot voor de flush constraint            Dim o3FaceProxy As FaceProxy        
        For Each o3FaceProxy In oExtrudeFeatureProxy.Faces 
        Dim oFPoint As Point
        oFPoint = o3FaceProxy.PointOnFace()
        If oFPoint.Y > 24 Then
        'MsgBox(oFPoint.X &",Y"&  oFPoint.Y & " Z" & oFPoint.Z)        o3Faces.Add(o3FaceProxy)
        End If
        Next


End While

End If

'Alle faces van het frame worden verzameld
oOccurrence = oAsmCompDef.Occurrences.Item(1)

Dim o2PartCompDef As PartComponentDefinition
o2PartCompDef = oOccurrence.Definition
Dim o2ExtrudeFeature As ExtrudeFeature
o2ExtrudeFeature = o2PartCompDef.Features.ExtrudeFeatures.Item(1)
Dim o2ExtrudeFeatureProxy As ExtrudeFeatureProxy
oOccurrence.CreateGeometryProxy (o2ExtrudeFeature, o2ExtrudeFeatureProxy)
Dim o2FaceProxy As FaceProxy
Dim o2FaceProxy2 As FaceProxy

'Filteren naar het onderste face van het frame voor de mate constraint    For Each o2FaceProxy In o2ExtrudeFeatureProxy.Faces
        Dim o2FPoint As Point 
        'MsgBox("3")        o2FPoint = o2FaceProxy.PointOnFace() 
        'MsgBox("4")        If o2FPoint.Z < -5.2 And o2FPoint.Z > -6 Then 
        'MsgBox("5")        ' MsgBox(o2FPoint.X &" Y="&  o2FPoint.Y & " Z=" & o2FPoint.Z)         o2FaceProxy2 = o2FaceProxy 
        'MsgBox("6")        End If
        Next  
    'Alle verzamelde bovenste faces van de poten en onderste face van het frame krijgen een mate constraint         
 For Each oFaceProxy In oFaces
     oAsmCompDef.Constraints.AddMateConstraint(o2FaceProxy2, oFaceProxy, 0)
    Next

'Filteren naar het face aan de zijkant van het frame voor de flush constraint  For Each o2FaceProxy In o2ExtrudeFeatureProxy.Faces
       Dim o2FPoint As Point
       o2FPoint = o2FaceProxy.PointOnFace()
       If o2FPoint.Y < 30 And o2FPoint.y > 25 Then
       'MsgBox(o2FPoint.X &" Y="&  o2FPoint.Y & " Z=" & o2FPoint.Z)       o2FaceProxy2 = o2FaceProxy
       End If
       Next
  'Alle verzamelde faces aan de zijkant van de poten en zijkant face van het frame krijgen een mate constraint            
  For Each o3FaceProxy In o3Faces
  'MsgBox("Error")  oAsmCompDef.Constraints.AddFlushConstraint(o2FaceProxy2, o3FaceProxy, 0)
  Next
  
Dim oLODRep As LevelofDetailRepresentation
oLODRep = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Master")
oLODRep.Activate

  For Each oOccurence In oAsmCompDef.Occurrences
  If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
  oOccurence.Grounded = False
  Else 
  End If
  Next
  

 

It inserts constraints on the correct faces in my assembly. It obtaints the correct faces with a filter right after each supportleg is placed. It stores the faces. At the end of the code all the collected faces that have passed the filter are mate constrained to the downside of the frame, wich has also has a face that is obtained though filtering.

 

The same goed for the flush constraint at the side of the supportleg to the side of the frame.

 

It isn't completely done yet, but the inserting of constraints works fine! Thanks T.S for you help 🙂

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

Post to forums  

Autodesk Design & Make Report