Journaling help needed
Journaling help needed
(OP)
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
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 




RE: Journaling help needed
For question #1, try this code (after changing the expression name you want to delete):
CODE
www.nxjournaling.com
RE: Journaling help needed
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 ModuleHope this helps.
Frank Swinkels
RE: Journaling help needed
CODE --> vb
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 ModuleRE: Journaling help needed
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
RE: Journaling help needed
RE: Journaling help needed
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