Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Journaling help needed

Status
Not open for further replies.

UGperson

Automotive
Mar 18, 2004
104
I have to questions.

How would I check for an expression and if it exists delete it?

how could I have an expression always equal the mass of the measured body?

Thanks

Code:
' NX 7.5.4.4
' Journal created by UGperson on Wed Mar 06 13:07:42 2013 Eastern Standard Time
'
Option Strict Off
Imports System
Imports NXOpen

Imports System.Collections
Imports NXOpen.UF


Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display



' ----------------------------------------------
'   Menu: Tools->Expression...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

'Dim expressionMassAttr As string

'Dim expressMass As string


'Dim expression1 As Expression = CType(workPart.Expressions.FindObject("MASS_ATTRIBUTE"), Expression)
'Check for expression "MASS_ATTRIBUTE"
Dim expressionMassAttr As Expression = CType(workPart.Expressions.FindObject("MASS_ATTRIBUTE"), Expression)


'workPart.Expressions.Delete(expression1)
'Delete expression "MASS_ATTRIBUTE"
workPart.Expressions.Delete(expressionMassAttr)





'Dim expression2 As Expression = CType(workPart.Expressions.FindObject("MASS"), Expression)
'Check for expression "MASS"
Dim expressMass As Expression = CType(workPart.Expressions.FindObject("MASS"), Expression)

'workPart.Expressions.Delete(expression2)
'Delete expression "MASS"
workPart.Expressions.Delete(expressMass)


Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId1)







' ----------------------------------------------
'   Menu: Analysis->Measure Bodies...
' ----------------------------------------------
Dim markIdMeasure As Session.UndoMarkId
markIdMeasure = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullNXObject As NXObject = Nothing

Dim measureBodyBuilder1 As MeasureBodyBuilder
measureBodyBuilder1 = workPart.MeasureManager.CreateMeasureBodyBuilder(nullNXObject)

theSession.SetUndoMarkName(markIdMeasure, "Measure Bodies Dialog")

'Dim body1 As Body = CType(workPart.Bodies.FindObject("BLOCK(1)"), Body)

Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelections() as NXObject
Dim myFaces as New ArrayList()
Dim myBodys as New ArrayList()
Dim i as Integer
Dim myFace as Face
Dim myBody as Body


        If (SelectFaces(mySelections) = Selection.Response.Ok) Then
            lw.Open
            lw.WriteLine(UBound(mySelections) + 1 & "Bodys selected")
            For each myEntity as NXObject in mySelections
				if myEntity.GetType().ToString = "NXOpen.Body" then
				
                    myBodys.Add(myEntity)
                end if
            Next
         Else
            lw.Open
            lw.WriteLine("Selection cancelled")
            lw.WriteLine("")
            lw.Close
        End If






Dim body1 As Body = myBody





Dim added1 As Boolean
added1 = measureBodyBuilder1.BodyObjects.Add(body1)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Measure Bodies")

Dim massUnits1(4) As Unit
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("SquareMilliMeter"), Unit)

massUnits1(0) = unit1
Dim unit2 As Unit = CType(workPart.UnitCollection.FindObject("CubicMilliMeter"), Unit)

massUnits1(1) = unit2
Dim unit3 As Unit = CType(workPart.UnitCollection.FindObject("Kilogram"), Unit)

massUnits1(2) = unit3
Dim unit4 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)

massUnits1(3) = unit4
Dim unit5 As Unit = CType(workPart.UnitCollection.FindObject("Newton"), Unit)

massUnits1(4) = unit5
Dim objects1(0) As IBody
objects1(0) = body1
Dim measureBodies1 As MeasureBodies
measureBodies1 = workPart.MeasureManager.NewMassProperties(massUnits1, 0.99, objects1)

Dim measure1 As Measure
measure1 = measureBodies1.CreateFeature()

measureBodies1.Dispose()
measureBodyBuilder1.BodyObjects.Clear()

theSession.DeleteUndoMark(markId2, Nothing)

theSession.SetUndoMarkName(markIdMeasure, "Measure Bodies")

measureBodyBuilder1.Destroy()

' ----------------------------------------------
'   Menu: Tools->Expression...
' ----------------------------------------------
Dim markIdExpression As Session.UndoMarkId
markIdExpression = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

Dim nullUnit As Unit = Nothing

'how do a mke p15 always = mass of measured body?
'Sets mass from kg to g
Dim expression1 As Expression
expression1 = workPart.Expressions.CreateWithUnits("MASS=p15*1000", nullUnit)


'sets the mass to be an attribute
Dim expression2 As Expression
expression2 = workPart.Expressions.CreateWithUnits("MASS_ATTRIBUTE=ug_setPartAttrValue( ""MASS"",format(""%0.2f"",mass))", nullUnit)

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markIdExpression)

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub


Function SelectFaces(ByRef selectedObjects() As NXObject) As Selection.Response

    Dim ui As UI = NXOpen.UI.GetUI
    Dim message As String = "Select Body"
    Dim title As String = "Selection"
    
    Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
    Dim keepHighlighted As Boolean = False
    Dim includeFeatures As Boolean = False
    Dim resp As Selection.Response
    
    Dim selectionAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    
    Dim selectionMask_array(1) As Selection.MaskTriple
    With selectionMask_array(1)
        .Type = UFConstants.UF_solid_type

    End With
    
    resp = ui.SelectionManager.SelectObjects(message, title, scope, selectionAction, includeFeatures, keepHighlighted, selectionMask_array, selectedObjects)
    
    Return resp
    
End Function




End Module
 
Replies continue below

Recommended for you

For question #2, if you create an associative body measurement, expressions will be created for you.

For question #1, try this code (after changing the expression name you want to delete):

Code:
[COLOR=blue]Option Strict Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpenUI  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Sub[/color] Main()  

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
        [COLOR=blue]Dim[/color] theUISession [COLOR=blue]As[/color] UI [COLOR=blue]=[/color] UI.GetUI  

        [COLOR=blue]For Each[/color] myexp [COLOR=blue]As[/color] Expression [COLOR=blue]In[/color] workPart.Expressions  
            [COLOR=blue]If[/color] [highlight #EDD400]myexp.Name [COLOR=blue]=[/color] "test"[/highlight] [COLOR=blue]Then[/color]  
                [COLOR=blue]Try[/color]  
                    workPart.Expressions.Delete(myexp)  
                [COLOR=blue]Catch[/color] ex [COLOR=blue]As[/color] NXException  
                    [COLOR=blue]If[/color] ex.ErrorCode [COLOR=blue]=[/color] 1050029 [COLOR=blue]Then[/color]  
                        [COLOR=green]'expression still in use[/color]
                        theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Cannot delete expression, it is still in use")  
                    [COLOR=blue]Else[/color]  
                        [COLOR=green]'other error[/color]
                        theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, ex.Message)  
                    End [COLOR=blue]If[/color]  
                End [COLOR=blue]Try[/color]  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Public Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As String[/color]) [COLOR=blue]As Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Here is another journal that may help you create what you are looking for.

The program works as follows:

1. Ask the user to select a body

2. The program checks if the body has a measurement. If it does not exists then a new measurebody is created.

3. Next check if we have an expression named MASS. If it exist we edit the expression so that it uses the measurement mass expression. If it does not exist then an expression is created using gram units.

I have not done anything about mass attribute.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features

Module MassUpdate
    Dim s As Session = Session.GetSession()
    Dim workPart As Part = s.Parts.Work

    Sub Main()
        Dim response0 As Selection.Response = Selection.Response.Cancel
        Dim body1 As Body = Nothing
        response0 = Selectbody(body1)
        If response0 = Selection.Response.Cancel Then GoTo end1
        ' Get the mass of the body if it exists if it does not exist then we create it
        Dim exps(-1) As Expression
        Dim boolean1 As Boolean
        boolean1 = GetBodyMassProperties(body1, exps)
        ' If the expressions exist do an update of mass and mass_attribute expressions
        ' If the expressions did not exist than create mass and mass_attribute expressions
        Dim massexp As Expression = Nothing
        Dim mass_attrexp As Expression = Nothing
        Dim massvalue As Double = Nothing
        Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("Gram"), Unit)
        Dim massrhs As String = Nothing
        ' here is the code for a mass expression
        Try
            ' mass expression exists
            massexp = workPart.Expressions.FindObject("MASS")
            workPart.Expressions.EditWithUnits(massexp, unit1, exps(2).Name)
        Catch ex As Exception
            ' mass expression did not exist
            massexp = workPart.Expressions.CreateWithUnits("MASS=" & exps(2).Name, unit1)
        End Try
end1:
    End Sub

    Public Function Selectbody(ByRef selectedObject As Body) As Selection.Response
        Dim ui As UI = ui.GetUI
        Dim message As String = "Select the body"
        Dim title As String = "Selection"
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim keepHighlighted As Boolean = True
        Dim includeFeatures As Boolean = True
        Dim response As Selection.Response
        Dim selectionAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim selectionMask_array(1) As Selection.MaskTriple
        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .Subtype = 0
            .SolidBodySubtype = 0
            .SolidBodySubtype = NXOpen.UF.UFConstants.UF_UI_SEL_FEATURE_BODY
        End With
        Dim cursor As Point3d
        response = ui.SelectionManager.SelectObject(message, title, scope, _
                                           selectionAction, includeFeatures, _
                                           False, selectionMask_array, _
                                           selectedObject, cursor)
        If response = Selection.Response.Cancel Or response = Selection.Response.Back Then
            Return Selection.Response.Cancel
        End If
        Return Selection.Response.Ok
    End Function
    Function GetBodyMassProperties(ByVal body1 As Body, ByRef exps() As Expression) As Boolean
        Dim boolean1 As Boolean = False
        ' need to check if body has body measurement
        Dim feat1() As Feature = body1.GetFeatures
        Dim measfeat1() As Feature
        For Each f As Feature In feat1
            measfeat1 = f.GetChildren
            For Each mf As Feature In measfeat1
                If mf.FeatureType = "BODY_MEASUREMENT" Then
                    ' get the expressions
                    exps = mf.GetExpressions
                    boolean1 = True
                Else
                    boolean1 = False
                End If
            Next
        Next
        ' if body does not contain a body measurement then we create one
        If boolean1 = False Then
            Dim nullNXObject As NXObject = Nothing
            Dim measureBodyBuilder1 As MeasureBodyBuilder
            measureBodyBuilder1 = workPart.MeasureManager.CreateMeasureBodyBuilder(nullNXObject)
            Dim added1 As Boolean
            added1 = measureBodyBuilder1.BodyObjects.Add(body1)
            Dim massUnits1(4) As Unit
            Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("SquareMilliMeter"), Unit)
            massUnits1(0) = unit1
            Dim unit2 As Unit = CType(workPart.UnitCollection.FindObject("CubicMilliMeter"), Unit)
            massUnits1(1) = unit2
            Dim unit3 As Unit = CType(workPart.UnitCollection.FindObject("Kilogram"), Unit)
            massUnits1(2) = unit3
            Dim unit4 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
            massUnits1(3) = unit4
            Dim unit5 As Unit = CType(workPart.UnitCollection.FindObject("Newton"), Unit)
            massUnits1(4) = unit5
            Dim objects1(0) As IBody
            objects1(0) = body1
            Dim measureBodies1 As MeasureBodies
            measureBodies1 = workPart.MeasureManager.NewMassProperties(massUnits1, 0.99, objects1)
            Dim measure1 As Measure
            measure1 = measureBodies1.CreateFeature()
            measureBodies1.Dispose()
            measureBodyBuilder1.BodyObjects.Clear()
            measureBodyBuilder1.Destroy()
            feat1 = body1.GetFeatures
            For Each f As Feature In feat1
                measfeat1 = f.GetChildren
                For Each mf As Feature In measfeat1
                    If mf.FeatureType = "BODY_MEASUREMENT" Then
                        ' get the expressions
                        exps = mf.GetExpressions
                        boolean1 = True
                    End If
                Next
            Next
        End If
        Return boolean1
    End Function
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function
End Module


Hope this helps.

Frank Swinkels
 
Thanks for the replies. I have modified the code that Frank posted to create an attribute called MASS. As long as the measure body is the last feature in your tree it will give you the total mass of the part in grams. We use this to generate our tittle block and parts list. I will attach the code incase anyone has a use for it.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features

Module MassUpdate
    Dim s As Session = Session.GetSession()
    Dim workPart As Part = s.Parts.Work

    Sub Main()
        Dim response0 As Selection.Response = Selection.Response.Cancel
        Dim body1 As Body = Nothing
        response0 = Selectbody(body1)
        If response0 = Selection.Response.Cancel Then GoTo end1
        ' Get the mass of the body if it exists if it does not exist then we create it
        Dim exps(-1) As Expression
        Dim boolean1 As Boolean
        boolean1 = GetBodyMassProperties(body1, exps)
        ' If the expressions exist do an update of mass and mass_attribute expressions
        ' If the expressions did not exist than create mass and mass_attribute expressions
        Dim massexp As Expression = Nothing
        Dim mass_attrexp As Expression = Nothing
        Dim massvalue As Double = Nothing
        Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("Gram"), Unit)
        Dim massrhs As String = Nothing
        ' here is the code for a mass expression the "*1000" converts to grams
        Try
            ' mass expression exists
            massexp = workPart.Expressions.FindObject("MASS")
            workPart.Expressions.EditWithUnits(massexp,unit1, exps(2).Name & "*1000")
        Catch ex As Exception
            ' mass expression did not exist the "1000*" converts to grams
            massexp = workPart.Expressions.CreateWithUnits("MASS=" & "1000*" & exps(2).Name, unit1)
        End Try


Dim nullUnit As Unit = Nothing



        Try
            ' mass_attribte expression exists
            mass_attrexp = workPart.Expressions.FindObject("MASS_ATTRIBUTE")
            'workPart.Expressions.EditWithUnits(mass_attrexp, unit1, exps(2).Name)
        Catch ex As Exception
            ' mass_attribte expression did not exist
            mass_attrexp = workPart.Expressions.CreateWithUnits("MASS_ATTRIBUTE=ug_setPartAttrValue( ""MASS"",format(""%0.2f"",mass))", nullUnit)
        End Try





end1:
    End Sub

    Public Function Selectbody(ByRef selectedObject As Body) As Selection.Response
        Dim ui As UI = ui.GetUI
        Dim message As String = "Select the body"
        Dim title As String = "Selection"
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim keepHighlighted As Boolean = True
        Dim includeFeatures As Boolean = True
        Dim response As Selection.Response
        Dim selectionAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim selectionMask_array(1) As Selection.MaskTriple
        With selectionMask_array(0)
            .Type = UFConstants.UF_solid_type
            .Subtype = 0
            .SolidBodySubtype = 0
            .SolidBodySubtype = NXOpen.UF.UFConstants.UF_UI_SEL_FEATURE_BODY
        End With
        Dim cursor As Point3d
        response = ui.SelectionManager.SelectObject(message, title, scope, _
                                           selectionAction, includeFeatures, _
                                           False, selectionMask_array, _
                                           selectedObject, cursor)
        If response = Selection.Response.Cancel Or response = Selection.Response.Back Then
            Return Selection.Response.Cancel
        End If
        Return Selection.Response.Ok
    End Function
    Function GetBodyMassProperties(ByVal body1 As Body, ByRef exps() As Expression) As Boolean
        Dim boolean1 As Boolean = False
        ' need to check if body has body measurement
        Dim feat1() As Feature = body1.GetFeatures
        Dim measfeat1() As Feature
        For Each f As Feature In feat1
            measfeat1 = f.GetChildren
            For Each mf As Feature In measfeat1
                If mf.FeatureType = "BODY_MEASUREMENT" Then
                    ' get the expressions
                    exps = mf.GetExpressions
                    boolean1 = True
                Else
                    boolean1 = False
                End If
            Next
        Next
        ' if body does not contain a body measurement then we create one
        If boolean1 = False Then
            Dim nullNXObject As NXObject = Nothing
            Dim measureBodyBuilder1 As MeasureBodyBuilder
            measureBodyBuilder1 = workPart.MeasureManager.CreateMeasureBodyBuilder(nullNXObject)
            Dim added1 As Boolean
            added1 = measureBodyBuilder1.BodyObjects.Add(body1)
            Dim massUnits1(4) As Unit
            Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("SquareMilliMeter"), Unit)
            massUnits1(0) = unit1
            Dim unit2 As Unit = CType(workPart.UnitCollection.FindObject("CubicMilliMeter"), Unit)
            massUnits1(1) = unit2
            Dim unit3 As Unit = CType(workPart.UnitCollection.FindObject("Kilogram"), Unit)
            massUnits1(2) = unit3
            Dim unit4 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
            massUnits1(3) = unit4
            Dim unit5 As Unit = CType(workPart.UnitCollection.FindObject("Newton"), Unit)
            massUnits1(4) = unit5
            Dim objects1(0) As IBody
            objects1(0) = body1
            Dim measureBodies1 As MeasureBodies
            measureBodies1 = workPart.MeasureManager.NewMassProperties(massUnits1, 0.99, objects1)
            Dim measure1 As Measure
            measure1 = measureBodies1.CreateFeature()
            measureBodies1.Dispose()
            measureBodyBuilder1.BodyObjects.Clear()
            measureBodyBuilder1.Destroy()
            feat1 = body1.GetFeatures
            For Each f As Feature In feat1
                measfeat1 = f.GetChildren
                For Each mf As Feature In measfeat1
                    If mf.FeatureType = "BODY_MEASUREMENT" Then
                        ' get the expressions
                        exps = mf.GetExpressions
                        boolean1 = True
                    End If
                Next
            Next
        End If
        Return boolean1
    End Function
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function
End Module
 
Just a note regarding expression units.

Say that the bodymeasurement of mass is in kg then if the expression uses the mass body measurement and the expression has the units set to grams then NX does the conversion. Therefore the "*1000" is not necessary. I think it is poor practise to use unitless values.

Frank Swinkels
 
Not sure if am doing something wrong or a bug in NX, but the only way I can have the MASS attribute so up in grams using this function "ug_setPartAttrValue( "MASS",format("%0.2f",mass))" is to multiply by 1000.
 
I think it is a limitation of the ug_setPartAttrValue function. I started a new inch part, set up a mass expression (in kg) and used the ug_setPartAttrValue function; no matter what, the value was converted to the default unit of the part file (lb-sec[sup]2[/sup]/in).

The good news is that NX8 and later really improves upon the expression/attribute link. You can create an expression with any mass unit and an attribute with any mass unit; when you link the two, the conversion is taken care of correctly.

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor