iLogic/Api Which model parameters are created by constraints.addflush??

iLogic/Api Which model parameters are created by constraints.addflush??

Will.Ehrendreich
Contributor Contributor
1,046 Views
4 Replies
Message 1 of 5

iLogic/Api Which model parameters are created by constraints.addflush??

Will.Ehrendreich
Contributor
Contributor

Hey all,

 

First of all, you guys are amazing, thank you for spending time helping people like this, it really makes this a great community to be a part of so I'm grateful for your time.

 

I have been trying like crazy to find the answer and I just can't figure it out, so hopefully it's something trivial I'm just missing.

 

I'm bravely diving into the wonderful and maddening world of automating assemblies.

 

When you use Constraints.AddFlush it creates a model parameter that corresponds to your newly created constraint.

I absolutely hate having them named useless things like "d89", so I'm of a mind to rename them something useful within the code.

The problem comes in that I'm creating a good many of them with each new ManagedComponentOccurrance I'm adding in, because naturally it makes sense to position and constrain them programmatically.

 

I would like an easy way to be able to get my model parameter being created at the same time as the constraint, so that I can rename the parameter.

 

Forgive my long iLogic code, I've been learning a lot so its about as inelegant as you might expect for someone learning.

 

imports system.IO
Imports System.Linq
Imports System.xml.linq
Imports System.data.linq



Sub Main
	Dim oTG = ThisApplication.TransientGeometry
	Dim doc = ThisDoc.Document
	Dim Path = ThisDoc.Path
	Dim ActiveDoc As AssemblyDocument
	ActiveDoc = ThisApplication.ActiveDocument
	Dim PurgeParams As Boolean
	PurgeParams = False
	Dim myParams As UserParameters
	myParams = ActiveDoc.ComponentDefinition.Parameters.UserParameters
	Dim myParam As UserParameter
	Dim myModParams As ModelParameters
	myModParams = ActiveDoc.ComponentDefinition.Parameters.ModelParameters
	Dim myModParam As ModelParameter


	Dim AsmComDef As AssemblyComponentDefinition
	AsmComDef = ActiveDoc.ComponentDefinition
	Dim di As DirectoryInfo = New DirectoryInfo(Path)
	Dim Part As ICadDoc

	Dim iLogicAuto = iLogicVb.Automation
	Dim ScallopFile As FileDescriptor

	Dim Scallop

	baseName = BaseNameOfScallop
	Dim ext As String
	ext = ".ipt"
	'MsgBox("Got Past setting di of " & di.ToString)
	Dim initialPart As ManagedComponentOccurrence


	ThisAssembly.BeginManage("initialScallop")
	initialPart = ThisAssembly.Components.Add(BaseNameOfScallop & ":1", ThisDoc.Path & "/" & "ScallopSupport.ipt", , True)

	ThisAssembly.EndManage


	'MsgBox(initialPart.tostring)
	Dim DirFilelist = di.GetFiles("*" & baseName & "*")
	'MsgBox("files with basename in directory : " & DirFilelist.GetLength(0).ToString)
	Dim OriginPoint As Point

	OriginPoint = oTG.CreatePoint(0, 0, 0)
	'MsgBox("Got past setting OriginPoint at " & OriginPoint.X & "," & OriginPoint.Y & "," & OriginPoint.Z)
	Dim OriPointToMatrix = oTG.CreateMatrix

	Try
		parameter("SeparationAmount") = parameter("SeparationAmount")
	Catch
		myParam = myParams.AddByExpression("SeparationAmount", "ZExtent / (NumOfScallopSupports - 1)", UnitsTypeEnum.kInchLengthUnits)
	End Try

	Dim myParamName As String

	Dim ParamSearch = (BaseNameOfScallop & "*" & "ZOffset")



	If NumOfScallopSupports > 0 Then
		ThisAssembly.BeginManage("Group 1")

		If NumOfScallopSupports = LastNumofScallopSupports Then

			PurgeParams = False
		Else
			PurgeParams = True
		End If
		If PurgeParams Then
			For Each myParam In myParams

				If myParam.Name Like ParamSearch Then
					myParam.Delete
				End If
			Next
			PurgeParams = False
		End If


		'ThisAssembly.Components.Add("ScallopSupport:1", "ScallopSupport.ipt", ThisAssembly.Document.ComponentDefinition.WorkPoints.Item(1), True, True)
		'MsgBox("Got past setting ScallopSupport:1 in Components.add")
		Dim AsmGeo As IGeometry
		AsmGeo = ThisAssembly.Geometry
		'MsgBox("Got past setting AsmGeo")

		Dim NumberReached = False
		Dim NextPartExists = False

		Dim PositionMatrix As Matrix
		PositionMatrix = oTG.CreateMatrix
		'MsgBox("Got past setting PositionMatrix Of " & PositionMatrix.Translation.ToString)




		'MsgBox("Got past setting SepAmmount Of "  & SeparationAmount )
		Dim TransformVector As Vector
		TransformVector = oTG.CreateVector(0, 0, 0)

		'if we have to have more than one Scallop..
		If NumOfScallopSupports >1 Then
			'clean up all Ilogic added parameters in this assembly. 



			'MsgBox("done deleting old params")

			'loop through every Scallop needed past the first one, which should already exist. 
			For i = 2 To NumOfScallopSupports
				'check if we have reached the end of our looping
				If Not NumberReached Then

					TransformVector.Z = SeparationAmount * i - SeparationAmount
					'MsgBox("Gotpast setting TransformVector Of " & TransformVector.X & "," & TransformVector.Y & "," & TransformVector.Z & " ")

					PositionMatrix.Translation.AddVector(TransformVector)

					'will this need to be pitched? if not everthing can be the same component. 
					If Pitched = True Then

						'--------------------------------------
						' I'll tackle this part later, I just want the single file version working before I worry about the automatic file duplication part. 

						'--------------------------------------
						'we need to make sure we have separate files for each, since values will not be the same.

						'					'MsgBox("basename is : " & baseName)
						'					Dim MyChar() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }
						'					baseName = baseName.TrimEnd(MyChar)
						'					baseName = baseName.Trim
						'					'MsgBox("basename trimmed is : " & baseName)


						'						'MessageBox.Show(baseName)

						'						Dim newName As String


						'						newName = baseName & DirFilelist.GetLength(0).ToString & ext

						'						'MessageBox.Show("NewName is set to: " & newName)
						'						Dim newFullPathAndName As String
						'						newFullPathAndName = ThisDoc.Path & "\" & newName
						'						MsgBox(newFullPathAndName)
						'						Dim info As New FileInfo(newFullPathAndName)
						'						Dim ID

						'						MsgBox(ThisAssembly.Components.Count & " components")

						'						iLogicVb.RunRule("ScallopSupport:1", "Duplicate")

						'						Dim ScallopUnique = Components.Add("ScallopSupport:" & i, "ScallopSupport" & i & ".ipt", position := PositionMatrix, grounded := False, visible := True, appearance := Nothing)

						'						MsgBox(ScallopUnique.Name)


					Else
						'this else assumes pitched is false

						'create all copies needed as geometry thats just an instance of the original scallopFile
						'add in the first one. 
						Scallop = Components.Add("ScallopSupport:" & i, "ScallopSupport.ipt", grounded := False, visible := True, appearance := Nothing)
						'MsgBox(Scallop.Name)
						Dim ScallopOccPartDef As PartComponentDefinition
						ScallopOccPartDef = Scallop.Occurrence.Definition

						'MsgBox(ScallopOccPartDef.MassProperties.Mass)
						Dim offsetParamName As String
						offsetParamName = Scallop.Name & "ZOffset"
						'MsgBox(offsetParamName)
						Dim offsetCalc As String
						offsetCalc = "SeparationAmount * " & i & " - SeparationAmount"
						'MsgBox(offsetCalc)

						'check if my offset user parameter is created, if not, create it. 
						Try
							parameter(offsetParamName) = parameter(offsetParamName)
							parameter(offsetParamName) = offsetCalc
							'MsgBox(parameter(offsetParamName) & " <- Already there. ")
						Catch

							myParam = AsmComDef.Parameters.UserParameters.AddByExpression(offsetParamName, offsetCalc, UnitsTypeEnum.kInchLengthUnits)
							'MsgBox(parameter(offsetParamName) & " <- had to create it")
						End Try



						Dim CompOcc As ComponentOccurrence
						CompOcc = AsmComDef.Occurrences.ItemByName(Scallop.Name)
						'MsgBox(CompArg.Name & " is the CompOcc name")
						Dim CompOccName As String
						CompOccName = CompOcc.Name

						Dim YZPlane As WorkPlane
						Dim XZPlane As WorkPlane
						Dim XYPlane As WorkPlane

						Dim YZPlaneProx As WorkPlaneProxy
						Dim XZPlaneProx As WorkPlaneProxy
						Dim XYPlaneProx As WorkPlaneProxy
						
						'-----------------------------
						'create my workplanes

						YZSubplane = ScallopOccPartDef.WorkPlanes(1)
						'MsgBox(YZSubplane.Name)

						XZSubplane = ScallopOccPartDef.WorkPlanes(2)
						'MsgBox(XZSubplane.Name)

						XYSubplane = ScallopOccPartDef.WorkPlanes(3)
						'MsgBox(XYSubplane.Name)
						
						'----------------------------
						'create my geometry proxies
						'
						AsmComDef.Occurrences.ItemByName(Scallop.Name).CreateGeometryProxy(YZPlane, YZPlaneProx)
						'MsgBox(YZSubplaneProx.Name)
						AsmComDef.Occurrences.ItemByName(Scallop.Name).CreateGeometryProxy(XZPlane, XZPlaneProx)
						'MsgBox(XZSubplaneProx.Name)
						AsmComDef.Occurrences.ItemByName(Scallop.Name).CreateGeometryProxy(XYPlane, XYPlaneProx)
						'MsgBox(XYSubplaneProx.Name)
						
						'add my constraints 

						YZConstraint = Constraints.AddFlush("FlushYZ" & Scallop.Name, "", "YZ Plane", CompOccName, YZPlaneProx.Name, 0)


						For Each myModParam In myModParams

							MsgBox(myModParam.Name & " is the mymodparam.name")
							Try
								If myModParam.AttributeSets.NameIsUsed(Scallop.Name & "AttSet") = True Then
								Else
									myModParam.AttributeSets.Add(Scallop.Name & "AttSet")
								End If
							Catch
							End Try


							MsgBox(myModParam.Dependents)


						Next
						'---------------------
						'rename the constraint. 
						'
						'How do I find the Index for this myModParams.Item([Index of the one I just created??]).name
						'Can I use a Linq? How would I do that?
						
						'						myModParams.Item.Name = "mFlushYZ" & Scallop.Name
						'						myModParams.Item("mFlushYZ" & Scallop.Name).Expression = 0

						XZConstraint = Constraints.AddFlush("FlushXZ" & i, "", "XZ Plane", CompArgName, XZPlaneProx.Name, 0)
						'						myModParams.Item().Name = "mFlushXZ" & Scallop.Name
						'						myModParams.Item("mFlushXZ" & Scallop.Name).Expression = 0

						XYConstraint = Constraints.AddFlush("FlushXY" & i, "", "XY Plane", CompArgName, XYPlaneProx.Name, parameter(offsetParamName))
						MsgBox(XYConstraint.Constraint.Name & "got past xyconstraint setting")
						'myModParams.Item().Name = "mFlushXY" & Scallop.Name
						'MsgBox(myModParams.Item(myModParams.Count).Name)
						'myModParams.Item(myModParam.).Expression = offsetParamName


					End If

					MsgBox("looping again")

				End If
				If i = NumOfScallopSupports Then
					NumberReached = True
					LastNumofScallopSupports = NumOfScallopSupports
					MsgBox("changed Lastnumofscallopsupports")
				End If
			Next
			MsgBox("got out of loop")
		End If


	End If

	ThisAssembly.EndManage("Group 1")
End Sub

 

 

Any help would be so amazing. Thanks!

 

0 Likes
Accepted solutions (1)
1,047 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

I normally avoid model parameters as much as possible, because they can dissapear on you when you do something like delete a dimension, sketch, feature, etc.  If I were you, I would just create a new UserParameter either within the code as needed, or beforehand.  That way you can name it whatever you want and give it whatever value you want, right from the start.  Then use those UserParameter instead of values within the code, whenever a value is needed.

On the flip side, did you know that when you place a new driving (or constraining) dimension, instead of just entering the numerical value you want it to be, you could enter something like "Height = 3.5" without the quotes, into the value box, and it will create a model parameter named Height with the value 3.5.  You may be able to use this functionality too, if it suits you needs.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

Will.Ehrendreich
Contributor
Contributor

Hey!

Thanks for your reply!

 

I actually am using both user parameters and model parameters to get done what I'm looking to do. I totally understand that the model parameters are created and destroyed with the parts or features that create them. I would actually just like a way to capture which parameter is created when something gets constrained, or see which parameters are tied to which constraints from inside iLogic.

 

I know I can manually go in and rename things and see which are tied to what from within the parameters dialog box, but I'm trying to automate the process of creation. I want to name everything something descriptive so that no matter where I look afterwards, I can see "breadcrumbs" that lead me to figure out what is going on. In my current iteration of my code, I actually have SORT OF managed something similar, but it's not as intuitive as I'd like.

 

The API Documentation seems to be fairly good in many respects, but I'm just not familiar with HOW to use a lot of it. I can see its there, but there isn't a whole lot of explanation of when and how and why to use a good bit of it. Like... for instance, it wasn't immediately clear to me that having a longer chain of something doesn't always give you proper access to that lower object, you have to kind of ... dim all the way down to something or it won't describe it correctly in intellisense, that sort of thing. 

 

I'm still trying to figure out how to typecast one object to another, where you take say, a managedcomponentoccurrence and cast it to a partcomponentoccurrence, or  even taking a more broadly typed Object entity, testing its type, then doing something with it as whatever it identifies as, etc. 

 

Also how to traverse multiple assembly levels is very elusive conceptually to me. Finding a part in the right context to affect the kinds of changes I want is .. confusing.

 

Anyway.. sorry for the rant all, I'm just having a difficult time without more senior programmers around, hahaha.

 

does anyone else have any suggestions about my original question though?

 

Will

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor
Accepted solution

When creating a flush constraint the last parameter does not have to be a value but can also be a expresion. You can use this to name the parameter while it is created. see the following example iLogic rule.

 

Dim doc As AssemblyDocument = ThisApplication.ActiveDocument

Dim face1 As Face = ThisApplication.CommandManager.Pick(
    SelectionFilterEnum.kPartFaceFilter, "Select Face1")
Dim face2 As Face = ThisApplication.CommandManager.Pick(
    SelectionFilterEnum.kPartFaceFilter, "Select Face1")

Dim occ1 As ComponentOccurrence = face1.Parent.Parent
Dim occ2 As ComponentOccurrence = face2.Parent.Parent

Dim faceProxie1 As FaceProxy = Nothing
Dim faceProxie2 As FaceProxy = Nothing

occ1.CreateGeometryProxy(face1, faceProxie1)
occ2.CreateGeometryProxy(face2, faceProxie2)

doc.ComponentDefinition.Constraints.AddFlushConstraint(faceProxie1, faceProxie2, "FlushParameterDistance = 10mm")

I have colored the expresion red here.

 

When you run this rule and select 2 faces a flush constraint will be created. Also there will be a model parameter called "FlushParameterDistance"

Jelte de Jong
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.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 5

Will.Ehrendreich
Contributor
Contributor

You're my hero, thank you so much!

I never considered to treat it like the box you enter the info into! That's Awesome! Thanks! I will try that out!

0 Likes