Update Multiple Model States

Update Multiple Model States

a81383
Enthusiast Enthusiast
654 Views
3 Replies
Message 1 of 4

Update Multiple Model States

a81383
Enthusiast
Enthusiast

Hello, 

 

I'm running into a couple of issues with the code shown below. Specifically within the "ModelStateUpdate" section.

Within my part (Box) I have a Model_State1 and a Model_State2 model states. They have a feature labeled "Cut". What I'm trying to do is suppress/unsuppress this feature within both model states from the assembly level. But for some reason it updates one but not the second.

 

Additionally, I don't like that the api converts my INCH parameter to MM when using param.Value. Is there a way to avoid this conversion? This is lower priority.

 

a81383_0-1690894910799.png

Sub ModelStateUpdate(oDef, oMS, oName)
    Dim userparams As UserParameters = oDef.Parameters.UserParameters
	For Each param In userparams
		MsgBox(oName & " | " & param.Name & " : " & param.Value)
	Next

	For Each oFeat As PartFeature In oDef.Features
		If oFeat.Name.Contains("Cut") Then
'			oFeat.Suppressed = False
			oFeat.Suppressed = True
			MsgBox(oName & " : " & oFeat.Name)
		End If
	Next
End Sub

 

0 Likes
Accepted solutions (2)
655 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @a81383.  When you have two or more components in the same assembly that both reference the same base file, but represent different ModelStates, that adds another level of complication into the mix that you have to compensate for.  You may be able to change the first one OK, but before you can do anything with the second one, you will need to update the document it references first, then attempt to do something with it.  Also, in a situation like this, you will likely not want to simply switch to the 'factory' version document of the component, because that may be a different document than what the component represents.  You may instead need to change which document is the factory document by attempting to activate the ModelState that the component is set to first, then that document should be the factory version, so that you can make changes to it.  If you are simply switching to the current factory document reference each time, you may simply be trying to change the same document multiple times, instead of trying to change the version of that file that the component actually represents.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

a81383
Enthusiast
Enthusiast

@WCrihfield 

Perfect! This sent me down the right path and I have it working perfectly now.

My last question is regarding the unit conversion from Inches to Centimeters, what is the best way to handle this conversion? Is the best way to just divide param.Value by 2.54 to get it back to inches? Although my concern with this is encountering rounding errors.

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor
Accepted solution

That has been a thorn in our sides almost since the beginning, but we can not change it.  The actual Parameter (or ModelParameter, UserParameter, ReferenceParameter, and so on) API objects have two main properties (Value & ModelValue) for returning their value, but both return the value in 'database units', which is centimeters for distance.  The simplest workaround in most cases is to use the Parameter.Expression.  That is always a String, instead of a Double or Integer, but it will be essentially what you see in the Equation column of the Parameters dialog.  The catch though, is that sometimes there may actually be an equation within the Expression, instead of just a simple numerical expression, and it will also always include the units specifier text.  There are two alternatives to using the API Parameter type objects, and they are the Parameter("ParameterName") expression, and the blue, unquoted parameter name within an internal (not external) rule.  In both of those two situations, it will represent the 'document units' value of the parameter.  But keep in mind that it is possible (but not common) for the 'document units' to be different from the units that the parameter itself is set to.

 

As for conversion...there are two main routes.  The simple math route, which you are considering, and the UnitsOfMeasure.ConvertUnits method.  Obviously the math route is quicker and simpler to use, but the math may not always be known, or accurate enough.  There are tons of examples of how to use the ConvertUnits method floating around this forum though.  The UnitsOfMeasure object itself can be obtained from ThisApplication.UnitsOfMeasure, or more accurately from the Document object itself, which may (in rare cases) be different from the application settings.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes