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: 

Creating constraints with code

5 REPLIES 5
Reply
Message 1 of 6
NachitoMax
399 Views, 5 Replies

Creating constraints with code

Hi

I need to programmatically add an assembly to my assembly and constrain it. I de ally I need 2 flush constraints and a constraint to the centre of an edge curve I select. Let's say I select the edge curve first, then the edge face and finally, the large top face. Can it be achieved? Will I need to set up imates?

Also, could it be done with multiple occurrences? I have made a routine that will add 9 parts in an array ( 1 in the centre and 4 each side.) I need them to be constrained



Any ideas?


Thanks

Niige

Nacho

Automation & Design Engineer

Inventor Programmer (C#, VB.Net / iLogic)


EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


5 REPLIES 5
Message 2 of 6
rjay75
in reply to: NachitoMax

If you have the entities selected (edge, face, etc...) on the occurrence then you need to create proxies of them and add the constraint.

 

If you have the objects face1 and face2 to are entities in occurrence1 and occurrence2 in assembly1

 

Dim face1Proxy As Object

Dim face2Proxy As Object

 

occurrence1.CreateGeometryProxy(face1, face1Proxy)

occurrence2.CreateGeometryProxy(face2, face2Proxy)

 

assembly1.ComponentDefinition.Constraints.AddFlushConstraint(face1Proxy, face2Proxy, 0.0)

 

Message 3 of 6
NachitoMax
in reply to: rjay75

Hi,

 

this is where i am so far..... I cant find any specific info on Proxies

 

Public Sub PlaceParts()
    
    
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
   
    ' Create a new matrix object.  It will be initialized to an identity matrix.
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    Dim GetPanel As ComponentOccurrence
    Set GetPanel = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "Select Part to work on")
    
    Dim face1  As Face
    Set face1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Edge")
    Dim face2  As Face
    Set face2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Face")
    
    Dim PartCompDef As PartComponentDefinition
    Set PartCompDef = GetPanel.Definition
    
    Dim sPartPath As String
    sPartPath = "C:\Users\NigelShaw\Documents\Inventor\Projects\SHAW DEVELOPMENT\MyAssemby.iam"
    
    Dim oMateiMateDefinition1 As MateiMateDefinition
    Set oMateiMateDefinition1 = PartCompDef.iMateDefinitions.AddMateiMateDefinition(face1, 0, , , "ST_C_EDGE")

    ' Create a mate iMateDefinition on a work axis
    Dim oMateiMateDefinition2 As MateiMateDefinition
    Set oMateiMateDefinition2 = PartCompDef.iMateDefinitions.AddMateiMateDefinition(face2, 0, , , "ST_F_FACE")
    
    ' Place the first occurrence.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmCompDef.Occurrences.Add(sPartPath, oMatrix)

    Dim oOcc2 As ComponentOccurrence
    Set oOcc2 = GetPanel
    
    ' Look through the iMateDefinitions defined for the first occurrence
    ' and find the one named "iMate:1".  This loop demonstrates using the
    ' Count and Item properties of the iMateDefinitions object.
    Dim i As Long
    Dim oiMateDef1 As iMateDefinition
    For i = 1 To oOcc1.iMateDefinitions.Count
        If oOcc1.iMateDefinitions.Item(i).Name = "ST_C_EDGE" Then
        MsgBox "Cam Edge is present"
            Set oiMateDef1 = oOcc1.iMateDefinitions.Item(i)
            Exit For
        End If
    Next
    
    Dim j As Long
    Dim oiMateDef2 As iMateDefinition
    For j = 1 To GetPanel.iMateDefinitions.Count
        If GetPanel.iMateDefinitions.Item(j).Name = "ST_F_EDGE" Then
        MsgBox "Panel Edge is present"
            Set oiMateDef2 = GetPanel.iMateDefinitions.Item(j)
            Exit For
        End If
    Next
    

    ' Create an iMate result using the two definitions.
    Dim oiMateResult As iMateResult
    Set oiMateResult = PartCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)

    
    

End Sub

 Doesnt matter what i do, i error on this line-

Set oiMateResult = PartCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)

 

the Assembly i place called "MyAssembly.iam" has 2 Flush iMates called ST_C_Face & ST_C_Edge. i am creating 2 flush iMates on my part that i select and these are called ST_F_EDGE & ST_F_FACE. i can get the iMate names to check that they are there, i just cannot make the imate. Is this because 1 set of iMates are in a part and the other set are in an assembly? do i need to move the iMates in the placed assembly to a part within that assembly?

 

 

thanks

 

Nigel

 

Nacho

Automation & Design Engineer

Inventor Programmer (C#, VB.Net / iLogic)


EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


Message 4 of 6
rjay75
in reply to: NachitoMax

You want to use the main assembly component definition.

 

Set oiMateResult = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)

Message 5 of 6
NachitoMax
in reply to: rjay75

Hi

I had tried that already and it didn't work. I got it working by using the iMateToEntity instead which works 2 out of 3 lol. I can constraint to my edge and my face but I cannot contrain to a work plane... The work plane is in the assembly. I have tried setting my pick to work plane but that doesn't work either. Any suggestins?


Thanks for your help


Nige

Nacho

Automation & Design Engineer

Inventor Programmer (C#, VB.Net / iLogic)


EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


Message 6 of 6
rjay75
in reply to: NachitoMax

Here's another method demonstrating proxies and constraints. If you can get logically (or by user selection) get to an entity and know the occurrence it belongs to you can create the constraints between the pairs.

 

Public Sub PlaceParts()    
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
   
    ' Create a new matrix object.  It will be initialized to an identity matrix.
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    Dim GetPanel As ComponentOccurrence
    Set GetPanel = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "Select Part to work on")
    
    'Selections from an assembly gets proxy objects
    Dim face1  As FaceProxy
    Set face1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Edge")
    Dim face2  As FaceProxy
    Set face2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Face")
	   
    Dim PartCompDef As PartComponentDefinition
    Set PartCompDef = GetPanel.Definition
    
    Dim sPartPath As String
    sPartPath = "C:\Users\NigelShaw\Documents\Inventor\Projects\SHAW DEVELOPMENT\MyAssemby.iam"

    ' Place the first occurrence.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmCompDef.Occurrences.Add(sPartPath, oMatrix)
	
'Get Workplane Proxies if workplanes are named. Dim wp1 As WorkPlane Dim wp1Proxy As WorkPlaneProxy Set wp1 = oOcc1.ComponentDefinition.WorkPlanes("namedPlane1") oOcc1.CreateGeometryProxy(wp1, wp1Proxy)
Dim wp2 As WorkPlane Dim wp2Proxy As WorkPlaneProxy Set wp2 = oOcc1.ComponentDefinition.WorkPlanes("namedPlane2") oOcc1.CreateGeometryProxy(wp2, wp2Proxy) 'Apply Constraints oAsmCompDef.ComponentDefinition.Constraints.AddMateConstraint(face1Proxy, wp1, 0.0) oAsmCompDef.ComponentDefinition.Constraints.AddMateConstraint(face2Proxy, wp2, 0.0) End Sub

 

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

Post to forums  

Autodesk Design & Make Report