Find Entities

Find Entities

a81383
Enthusiast Enthusiast
788 Views
5 Replies
Message 1 of 6

Find Entities

a81383
Enthusiast
Enthusiast

 

Hello, I'm trying to find constraint entities and use them to create additional constraints.

 

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	
	For Each c As AssemblyConstraint In oAsm.ComponentDefinition.Constraints
			
			Dim oOcc1 As ComponentOccurrence = c.AffectedOccurrenceOne
			Dim oOcc2 As ComponentOccurrence = c.AffectedOccurrenceTwo
			Dim oOccDoc1 As Document = oOcc1.Definition.Document
			Dim oOccDoc2 As Document = oOcc2.Definition.Document
		
			Dim oProx As Object
		
			oName1 = iLogicVb.Automation.GetNamedEntities(oOccDoc1).FindEntity(c.EntityOne.NativeObject)
			oEntity1 = oOcc1.CreateGeometryProxy(oName1, oProx)
			
			oName1 = iLogicVb.Automation.GetNamedEntities(oOccDoc2).FindEntity(c.EntityTwo.NativeObject)
			oEntity2 = oOcc2.CreateGeometryProxy(oName2, oProx)
Next

 

0 Likes
Accepted solutions (2)
789 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @a81383.  I'm not sure what you plan for this code to accomplish right now, but I do see a few things that do not look correct.  The FindEntity method is looking for a String (text type value) as Input, not an Object, and returns an Object, not a String.  Also, the CreateGeometryProxy method is a Sub routine, and does not 'return' any value, but instead it is meant to 'set' a value to the second input object, which will be one of the specific types of 'proxy' objects.  So, to use that CreateGeometryProxy method, you first declare a variable, similar to your oProx variable, which will not have a value yet, just for use as the second input variable in that method.  Then you supply that variable as the second input variable, to that method.  Then when that method runs, it sets the value to that second input variable.  However, that is not needed in this case, because the Constraint.EntityOne or Constraint.EntityTwo object should already be the 'proxy' type object.  Then you can use that Entity.NativeObject to get the real object in the context of the part or sub assembly.  So, I eliminated the FindEntity methods, and the CreateGeometryProxy methods, and laid out the code the way I assume you wanted it.  But as I said before, I am not really sure what your goal with this bit code is right now, so it is not really doing anything productive yet.

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oConstraints As AssemblyConstraints = oADef.Constraints
Dim oConst As AssemblyConstraint
For Each oConst In oConstraints
	Dim oOcc1 As ComponentOccurrence = oConst.AffectedOccurrenceOne
	Dim oOcc2 As ComponentOccurrence = oConst.AffectedOccurrenceTwo
	Dim oOccDoc1 As Document = oOcc1.Definition.Document
	Dim oOccDoc2 As Document = oOcc2.Definition.Document
	oEntity1Proxy = oConst.EntityOne
	oEntity1 = oConst.EntityOne.NativeObject
	oEntity2Proxy = oConst.EntityTwo
	oEntity2 = oConst.EntityTwo.NativeObject
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

a81383
Enthusiast
Enthusiast

Hi @WCrihfield

 

Right now I'm just trying to read the information for each constraint, delete it, and re-add them all.

 

I'll keep looking at it. Buts its still struggling to find the surfaces. I'll look into what a "Proxy" is as well. I'm not very familiar with them.  

 

Thank You!

 

		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
		Dim oConstraints As AssemblyConstraints = oADef.Constraints
		Dim oConst As AssemblyConstraint
		For Each oConst In oConstraints
			Dim oOcc1 As ComponentOccurrence = oConst.AffectedOccurrenceOne
			Dim oOcc2 As ComponentOccurrence = oConst.AffectedOccurrenceTwo
			Dim oOccDoc1 As Document = oOcc1.Definition.Document
			Dim oOccDoc2 As Document = oOcc2.Definition.Document
			oEntity1Proxy = oConst.EntityOne
			oEntity1 = oConst.EntityOne.NativeObject
			oEntity2Proxy = oConst.EntityTwo
			oEntity2 = oConst.EntityTwo.NativeObject


			oConstraintName = oConst.Name
			oOffset = oConst.Offset.Name
			oOccurrenceOne = oConst.OccurrenceOne.Name
			oOccurrenceTwo = oConst.OccurrenceTwo.Name
'			MsgBox(oOccurrenceOne)
'			MsgBox(oOccurrenceTwo)

			oConstraintType = Left(oConstraintName, 1)
'			MsgBox(oConstraintName)
			
			oConst.Delete
'			Logger.Warn("Invalid contraint: {1}{0}Occurrences:{0}{2}{0}{3}", vbCrLf, oConst.Name, oConst.OccurrenceOne.Name, oConst.OccurrenceTwo.Name)
			
			Select Case oConstraintType
			Case "M"
				Constraints.AddMate(oConstraintName, oOccurrenceOne, oEntity1Proxy, oOccurrenceTwo, oEntity2Proxy, oOffset)	
			Case "F"
				Constraints.AddFlush(oConstraintName, oOccurrenceOne, oEntity1Proxy, oOccurrenceTwo, oEntity2Proxy, oOffset)			
			End Select
		Next

 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @a81383.  A proxy object is sort of like a copy of the original geometry, that gets created when put components into an assembly, and that only exists within the assembly.  This copied version of the geometry object is the only geometry from the component that really exists directly within the assembly, and is sort of transformed from the component's model space to the assemblies own model space when it is copied.  In an assembly, there is no original, native geometry other than things like work features (WorkAxes, WorkPlanes, WorkPoints, etc), and any sketch geometry that was created directly within  the assembly itself.  All 'real' geometry in an assembly belongs to the components within the assembly, not the assembly itself.  So, within an assembly, any geometry you see and can click on, like a Face, Edge, or Vertex (visible geometry of components) are actually things like FaceProxy, EdgeProxy, VertexProxy, which are like copies of the original geometry objects.  These proxy versions of objects only exist within the assembly, and are used for things like measurements, and constraints.  Since these proxy objects all co-exist directly within the assembly's 3D space (3D coordinate system of the main assembly), they can be measured between, and constrained to each other.  When you think of having a part document open and active on your screen, you know you can not measure between one of its faces and a face on another part in another part file, because the two parts are in different/separate 3D coordinate systems, and the two systems can't relate to each other.  The components you put into an assembly are just copied instances (proxy) of the original geometry of the parts.  Also, while in a main assembly, a component that is down within another sub-assembly, is also known as a ComponentOccurrenceProxy, instead of a regular ComponentOccurrence, because it is sort of like a copy of a copy, from the perspective of the main assembly.  Most specific proxy type objects have a property called NativeObject, which will usually return the geometry object that they are a copy of (its source).  If the proxy object represents the face of a top level part type component in the main assembly, then the NativeObject of that FaceProxy object will be the original Face object within the context (3d coordinate space) of the original part file.  Does that make more sense now?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @a81383.  I have another iLogic rule that you may find helpful, that is along the same train of thought.  It does not delete, then re-create each constraint, but it does gather information about each constraint and report its findings to you, to give you more insight about them that can help you better understand what to expect from the various specific types of constraints, and their properties.  This code references and contains a custom Function that I created at some point in the past, which tests which of the more specific types of constraint it is, then returns the parameter object which controls the constraint, from the unique properties that are only exposed under those more specific types of constraint objects.

Warning:  This will show a message for each constraint in your assembly, so if there are tons of them, it might take a while to get through them all.

Here is the code:

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim oConsts As AssemblyConstraints = oADef.Constraints
	For Each oConst As AssemblyConstraint In oConsts
		'if the constraint is suppressed, then skip to next constraint
		'can't access/do much with a suppressed constraint
		If oConst.Suppressed Then Continue For
		Dim oOcc1 As ComponentOccurrence = oConst.OccurrenceOne
		Dim oOcc2 As ComponentOccurrence = oConst.OccurrenceTwo
		Dim oEnt1 As Object = oConst.EntityOne
		Dim oEnt2 As Object = oConst.EntityTwo
		Dim oGeom1 As Object = oConst.GeometryOne
		Dim oGeom2 As Object = oConst.GeometryTwo
		'some types of constraints do not return a parameter, so below may = Nothing
		Dim oParam As Inventor.Parameter = GetParamFromConstraint(oConst)
		Dim oParamName As String = ""
		Dim oParamExp As String = ""
		If oParam IsNot Nothing Then
			oParamName = oParam.Name
			oParamExp = oParam.Expression
		End If
		Dim oReport As String = "Constraint Name:  " & oConst.Name
		oReport = oReport & vbCrLf & "Constraint Type:  " & TypeName(oConst)
		oReport = oReport & vbCrLf & "Component 1 Name:  " & oOcc1.Name
		oReport = oReport & vbCrLf & "Component 2 Name:  " & oOcc2.Name
		oReport = oReport & vbCrLf & "Entity 1 Type:  " & TypeName(oEnt1)
		oReport = oReport & vbCrLf & "Entity 2 Type:  " & TypeName(oEnt2)
		oReport = oReport & vbCrLf & "Geometry 1 Type:  " & TypeName(oGeom1)
		oReport = oReport & vbCrLf & "Geometry 2 Type:  " & TypeName(oGeom2)
		oReport = oReport & vbCrLf & "Controlling Parameter Name:  " & oParamName
		oReport = oReport & vbCrLf & "Controlling Parameter Expression:  " & oParamExp
		MsgBox(oReport, vbInformation, "Constraint Information")
	Next
End Sub

Function GetParamFromConstraint(oConst As AssemblyConstraint) As Inventor.Parameter
	Dim oParam As Inventor.Parameter = Nothing
	If TypeOf oConst Is AngleConstraint Then
		Dim oAngleConstraint As AngleConstraint = oConst
		oParam = oAngleConstraint.Angle
	ElseIf TypeOf oConst Is FlushConstraint Then
		Dim oFlushConstraint As FlushConstraint = oConst
		oParam = oFlushConstraint.Offset
	ElseIf TypeOf oConst Is InsertConstraint Then
		Dim oInsertConstraint As InsertConstraint = oConst
		oParam = oInsertConstraint.Distance
	ElseIf TypeOf oConst Is MateConstraint Then
		Dim oMateConstraint As MateConstraint = oConst
		oParam = oMateConstraint.Offset
	ElseIf TypeOf oConst Is RotateRotateConstraint Then
		Dim oRotateRotateConstraint As RotateRotateConstraint = oConst
		oParam = oRotateRotateConstraint.Ratio
	ElseIf TypeOf oConst Is RotateTranslateConstraint Then
		Dim oRotateTranslateConstraint As RotateTranslateConstraint = oConst
		oParam = oRotateTranslateConstraint.Distance
	ElseIf TypeOf oConst Is AssemblySymmetryConstraint Then
		Dim oAssemblySymmetryConstraint As AssemblySymmetryConstraint = oConst
		'there is no Parameter involved with this type of constraint
	ElseIf TypeOf oConst Is TangentConstraint Then
		Dim oTangentConstraint As TangentConstraint = oConst
		oParam = oTangentConstraint.Offset
	ElseIf TypeOf oConst Is TransitionalConstraint Then
		Dim oTransitionalConstraint As TransitionalConstraint = oConst
		'there is no Parameter involved with this type of constraint
	End If
	Return oParam
End Function

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

a81383
Enthusiast
Enthusiast

This has been a lot of great information. Thank you @WCrihfield!  I never really thought about how it pulls solids into the assembly level. But that makes perfect sense with the proxy's. I'll continue to look into proxy's and see if I can get these constraints to update. But this has been a huge help. Thanks again!

0 Likes