Get all dimensions with formula set as value

Hello

Is there a way to get all dimensions of a part that have a formula set as value? Or even better; That use an expression in the formula? This should work if the dimension is still named p** or is a named expression.

If not, can you tell me how to get a list of all expressions? I might figure something out that fits my case.

Thank you and regards

By part dimensions do you mean sketch dimensions? If so, you can use the sketch's .GetAllExpressions method. Once you have an expression, there are a few ways to check the formula. You could parse the .RightHandSide for mathematical operators, or you could compare the .Value of the expression to the .RightHandSide. If the expression just uses a simple value, the value (converted to a string) will equal the .RightHandSide.

An article about expressions (with code) can be found here:
http://www.nxjournaling.com/content/expressions-query-existing-expressions

THank you for your reply! I should have added that i use visual basic. I can't find documentation on .GetAllExpressions, only for Python.
So If I want all expressions that are used in a sketch I'd first have to find all sketches and use .GetAllExpressions, right? How can I do that with visual basic?

What version of NX are you using?

The .GetAllExpressions method should be listed in the .net documentation. The NX 12 documentation is particularly bad, but it gets slightly better in newer versions.

https://docs.plm.automation.siemens.com/data_services/resources/nx/12/nx...

I'm using the newest NX version.
How do I loop trough all sketches though? Internal and external ones.
Sorry, I'm new to this and I struggle with the API documentation in most cases.
You've been a lot of help so far.

Each part has a .Sketches collection that you can iterate through. The code would look something like:

For each thisSketch as Sketch in myPart.Sketches
dim myExpressions() as Expression
myExpressions = thisSketch.GetAllExpressions
'code to process expressions
Next

Is it also possible to loop trough every construction element, check if it's surpressed, if not get the expressions and if it has a sketch get the expressions of the sketch?
If I use your code I get the expressions for every sketch, even if they're not in use. Also the start and end values for an extrude are missing for example.

By "construction element" do you mean the features (the entries in the part navigator)? If so, the answer is "yes"; the code to do so would look something like below:

for each tempFeature as Features.Feature in theSession.Parts.Work.Features
'process the feature
next

Many feature types use "builder objects" that you can use to edit the feature or get more info than the feature itself provides. One strategy would be to check the type of the feature, then use the corresponding builder to query the values you need. Some parameters may not have numerical values; an extrude feature, for example, can have limits such as "to next" or "through all".

Thanks!
For me the important thing are sketches. I got the extrude builder working but didn't find anything useful for sketches. I have to get all dimensions or convert all to driving dimension.
At the moment my entire script is flawed if there is a dimension that has no expression.

I just saw that there is an Information window when you right click on a sketch. In field Status it either says "Fully-coinstrained with auto dimensions" or just "Fully-constrained". If there isn't a way to convert all dimensions, can I at least get to this information of a sketch? Then I could only run it on the parts that have no sketches with "Fully-coinstrained with auto dimensions" and give the user an alert that he should correct that.

The short answer is "yes"; you can get all the dimensions from a sketch and check to see if they are driving, auto, or reference dimensions. Any auto dimensions can be converted to driving. I'll try to post some code as soon as I get more time to work on it.

It sounds like you are creating a utility to check the model and alert the user to issues found (such as under-constrained sketches). If so, note that there is functionality in NX called "check mate" that can perform many such checks on the file and alert the user. There are many checks that come with NX that you can run as-is, such as checking that all sketches are fully constrained. If you have a check mate license, you can write custom checks for your files. I'd suggest looking into the standard check mate tests, NX might have what you need already built in.

https://docs.plm.automation.siemens.com/tdoc/nx/1899/nx_help#uid:xid1128...

That sounds promising. The general function of my script hasn't anything to do with checking, no. I just need all dimensions for my functions, otherwise it could end in a wrong result. So I want to make sure that I get all of them.
If it wasn't possible to get auto dimensions I would alert the user so he can fix the model. But thats the worst case scenario.
Would be awesome if you could show me how to get all dimensions and even convert them to driving. Thank you for all the help you're providing!

The code below will look at the sketches in the work part, report dimension types, and attempt to convert automatic dimensions to driving dimensions.

'NXJournaling.com
'February 21, 2020

'report sketch dimensions (driving, automatic, and reference).
'convert auto dims to driving dims.

Option Strict Off

Imports System

Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF

Module sketch_dim_type_2
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "sketch auto dims to driving")

lw.Open()

For Each tempSketch As Sketch In theSession.Parts.Work.Sketches
If tempSketch.IsDraftingSketch Then
'skip drafting sketches
Continue For
End If

Dim sketchFeature As Features.SketchFeature = tempSketch.Feature
lw.WriteLine(sketchFeature.GetFeatureName)
Dim dofNeeded As Integer
lw.WriteLine("sketch status: " & tempSketch.GetStatus(dofNeeded).ToString)
Dim theDimConstraints() As SketchConstraint = tempSketch.GetAllConstraintsOfType(NXOpen.Sketch.ConstraintClass.Dimension, NXOpen.Sketch.ConstraintType.NoCon)

For Each aDimCon As SketchDimensionalConstraint In theDimConstraints
Dim theAssocDim As Annotations.Dimension = aDimCon.AssociatedDimension
lw.WriteLine(" Dimension Status: " & aDimCon.DimensionState.ToString)
If aDimCon.DimensionState = SketchDimensionalConstraint.DimensionStateType.Automatic Then
tempSketch.Activate(Sketch.ViewReorient.False)
Dim convertToFromReferenceBuilder1 As NXOpen.ConvertToFromReferenceBuilder
convertToFromReferenceBuilder1 = theSession.Parts.Work.Sketches.CreateConvertToFromReferenceBuilder()

Dim selectNXObjectList1 As NXOpen.SelectNXObjectList
selectNXObjectList1 = convertToFromReferenceBuilder1.InputObjects

Dim added1 As Boolean
added1 = selectNXObjectList1.Add(theAssocDim)
convertToFromReferenceBuilder1.OutputState = NXOpen.ConvertToFromReferenceBuilder.OutputType.Active
Dim nXObject1 As NXOpen.NXObject
nXObject1 = convertToFromReferenceBuilder1.Commit()

convertToFromReferenceBuilder1.Destroy()
theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.False, NXOpen.Sketch.UpdateLevel.SketchOnly)
lw.WriteLine(" Dimension converted to driving")
End If
Next
lw.WriteLine("")
Next

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

End Function

End Module

Awesome, thank you!