Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

How to prevent multiplying the same constraints?

Odalv
Contributor

How to prevent multiplying the same constraints?

Odalv
Contributor
Contributor

Hello! 
When I'm making one type of assembly, I need to constrain many parts to the Z axis after placing them. So, I was able to compose some small code to do this task.

 

'Select the main assembly Z axis    
    
    'Set a reference to the assembly component definintion.
    Dim oACD As AssemblyComponentDefinition
    oACD = ThisApplication.ActiveDocument.ComponentDefinition

    'Get the Z Axis of the assembly
    Dim oZAxis1 As WorkAxis
    oZAxis1 = oACD.WorkAxes.Item("Z Axis")
	
'Select the part Z axis
    
    'Checking parts
    Dim oZAxis2 As WorkAxis
    Dim oOcc As ComponentOccurrence
    Dim ozProxy As WorkAxisProxy
    For Each oOcc In oACD.Occurrences
    
    'Get the Z axis from the part
    oZAxis2 = oOcc.Definition.WorkAxes.Item("Z Axis")
    Call oOcc.CreateGeometryProxy(oZAxis2,ozProxy)
    oACD.Constraints.AddMateConstraint(oZAxis1, ozProxy, 0)
    Next
	

 


If I place 100 of these parts and I run the code, it works - every part has its own constraint to the Z axis of assembly. That's a total of 100 constraints on the Z axis.
But if I place just one part after these 100 and run the same code again, every one of those 100 gets another constraint to the Z axis, so the same constraint is multiplying for already constrained parts.
Is there maybe some way to prevent this from happening?

0 Likes
Reply
Accepted solutions (1)
383 Views
4 Replies
Replies (4)

WCrihfield
Mentor
Mentor

Hi @Odalv.  That's a tough one to avoid.  If the component was already fully constrained, then you attempted to add more constraints to it, then it might throw an error, which we could handle inside of a Try...Catch block, then simply not add the constraint, but when it is not yet fully constrained, it will sometimes allow a similar constraint to be added again.  It may just point that redundancy out to you after the fact, at some point later.  I was going to suggest using a naming convention to assign a name to each one as you are creating them, then if the code runs again, check to see of that component has a constraint on it with a name matching that pattern, and if so, skip adding another to that component.  However, that may be too difficult to manage in a large assembly, so I thought of simply checking the constraints on each component, to see if it already has a 'Mate' type constraint on it, which includes either the assembly's Z-Axis, or the Z-Axis proxy of the component.  If one is found, then skip adding another to that component.  Below is a quick draft of this idea in code, but I have not tested it yet.

 

Dim oACD As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim oCostraints As AssemblyConstraints = oACD.Constraints
Dim oAsmZAxis As WorkAxis = oACD.WorkAxes.Item("Z Axis")
Dim oOcc As ComponentOccurrence
Dim oOccZAxis As WorkAxis
Dim oOccZAxisProxy As WorkAxisProxy
Dim oMate As MateConstraint
For Each oOcc In oACD.Occurrences
	oOccZAxis = oOcc.Definition.WorkAxes.Item("Z Axis")
	oOcc.CreateGeometryProxy(oOccZAxis, oOccZAxisProxy)
	Dim oExistingFound As Boolean = False
	If oOcc.Constraints.Count > 0 Then
		For Each oConst As AssemblyConstraint In oOcc.Constraints
			If oConst.Type = ObjectTypeEnum.kMateConstraintObject Then
				Dim oMConst As MateConstraint = oConst
				If oMConst.EntityOne Is oOccZAxis Or _
					oMConst.EntityOne Is oOccZAxisProxy Then
					oExistingFound = True
					Exit For
				End If
			End If					
		Next
	End If
	If Not oExistingFound Then
		oMate = oCostraints.AddMateConstraint(oAsmZAxis, oOccZAxisProxy, 0)
	End If
Next

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Odalv
Contributor
Contributor

Thank you @WCrihfield

I have run your code and there was no error. Unfortunately, it seems the problem still exists. Constraint to Z Axis is multiplying for all the parts that are already constrained to Z Axis.

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Odalv.  I realized later that my section of code to 'check' existing constraints was not thorough enough yet.  It is only checking EntityOne (and not EntityTwo) of the constraint, plus it is only looking for the 'axis' objects obtained from the component, and not looking for the axis object from the main assembly.  I think the easiest way to update that part of the existing code would be to simply add one more line of code in there that checks for the main assembly's Z-axis as being the value of EntityOne.  Those should be the only 3 objects that could possibly be on one side of the constraint, if the constraint already exists.  And actually, the way we are creating the constraint, I believe that the main assembly's Z-axis should always be the value of EntityOne, so I'm hoping this additional check is the key that fixes this for you.

Dim oACD As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim oCostraints As AssemblyConstraints = oACD.Constraints
Dim oAsmZAxis As WorkAxis = oACD.WorkAxes.Item("Z Axis")
Dim oOcc As ComponentOccurrence
Dim oOccZAxis As WorkAxis
Dim oOccZAxisProxy As WorkAxisProxy
Dim oMate As MateConstraint
For Each oOcc In oACD.Occurrences
	oOccZAxis = oOcc.Definition.WorkAxes.Item("Z Axis")
	oOcc.CreateGeometryProxy(oOccZAxis, oOccZAxisProxy)
	Dim oExistingFound As Boolean = False
	If oOcc.Constraints.Count > 0 Then
		For Each oConst As AssemblyConstraint In oOcc.Constraints
			If oConst.Type = ObjectTypeEnum.kMateConstraintObject Then
				Dim oMConst As MateConstraint = oConst
				If oMConst.EntityOne Is oOccZAxis Or _
					oMConst.EntityOne Is oOccZAxisProxy Or _
					oMConst.EntityOne Is oAsmZAxis Then
					oExistingFound = True
					Exit For
				End If
			End If					
		Next
	End If
	If Not oExistingFound Then
		oMate = oCostraints.AddMateConstraint(oAsmZAxis, oOccZAxisProxy, 0)
	End If
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Odalv
Contributor
Contributor

Big thank to you @WCrihfield! It works 😀 I really appreciate your help. This one saved me a lot of time!

 

 

0 Likes