Forums:
Below is the journal for assigning material to the part:
Upon running material to be assigned is keyed in through windows form.
Also this journal created measure body expression and i have tried to capture the expression with unit as 'kg' and rename the expression as 'Mass' and Mass attribute is added to the attribute properties.
This macro is running fine. But i want to improvise few things.
1.) i want to check if any material is assigned previously and delete if any material is added.
2.) also the macro is failing sometimes if there are so many expressions in the part already. While running the search querf for expression with units 'kg' the macro is failing.
Could you please help in updating the journal.
' NX 12.0.1.7' Journal created by Balaji_Sampath03 on Fri Mar 13 16:11:05 2019 India Standard Time'Imports System Imports NXOpen Imports System.Collections.GenericImports NXOpen.UFImports NXOpenUI Module NXJournal Sub Main (ByVal args()AsString) Dim theSession As NXOpen.Session= NXOpen.Session.GetSession()Dim workPart As NXOpen.Part= theSession.Parts.WorkDim displayPart As NXOpen.Part= theSession.Parts.Display ' ----------------------------------------------' Menu: Tools->Materials->Assign Materials...' ---------------------------------------------- Dim MaterialListBuilder1 As NXOpen.PhysMat.PhysicalMaterialListBuilder=Nothing MaterialListBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder() Dim MaterialAssignBuilder1 As NXOpen.PhysMat.PhysicalMaterialAssignBuilder=Nothing MaterialAssignBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateMaterialAssignBuilder() Dim physicalMaterialListBuilder3 As NXOpen.PhysMat.PhysicalMaterialListBuilder=Nothing physicalMaterialListBuilder3 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder() Dim nXObject1 As NXOpen.NXObject=NothingDim physicalMaterial1 As NXOpen.PhysicalMaterial=CType(nXObject1, NXOpen.PhysicalMaterial) Dim MaterialToAssign As NXOpen.PhysicalMaterial=NothingDim MaterialToAdd AsString=Nothing 'create new form objectDim myForm AsNew AddMaterial 'set form object properties (current part attribute title and value) myForm.MaterialToAdd= MaterialToAdd 'display our form myForm.ShowDialog() If myForm.CanceledThen'user pressed cancel, exit journalReturnElse'user pressed OK, assign value from form to part attribute MaterialToAdd = myForm.MaterialToAdd EndIf MaterialToAssign = workPart.MaterialManager.PhysicalMaterials.LoadFromMatmlLibrary("Z:\Knowldege Management\BOK\References\RTCOE_MatLibrary\physicalmateriallibrary.xml", MaterialToAdd) Dim physicalMaterialBuilder1 As NXOpen.PhysicalMaterialBuilder=Nothing physicalMaterialBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreatePhysicalMaterialInspectBuilder(MaterialToAssign) physicalMaterialBuilder1.Destroy() Dim theBody As Body If SelectSolidToAssignMaterial("select a body", theBody)= Selection.Response.CancelThenReturnEndIf Dim theBodies(0)As Body theBodies(0)= theBody MaterialToAssign.AssignObjects(theBodies) MaterialListBuilder1.Destroy() MaterialAssignBuilder1.Destroy() ' ----------------------------------------------' Menu: Analysis->Measure Body...' ---------------------------------------------- 'Dim MeasureToDelete As NXOpen.Measure = DeleteMeasure() Dim nullNXOpen_NXObject As NXOpen.NXObject=NothingDim measureBodyBuilder1 As NXOpen.MeasureBodyBuilder=Nothing measureBodyBuilder1 = workPart.MeasureManager.CreateMeasureBodyBuilder(nullNXOpen_NXObject) Dim BodyToMeasure(0)As NXOpen.Body BodyToMeasure(0)= theBody Dim bodyDumbRule1 As NXOpen.BodyDumbRule=Nothing bodyDumbRule1 = workPart.ScRuleFactory.CreateRuleBodyDumb(BodyToMeasure, True) Dim rules1(0)As NXOpen.SelectionIntentRule rules1(0)= bodyDumbRule1 measureBodyBuilder1.BodyCollector.ReplaceRules(rules1, False) Dim massUnits1(4)As NXOpen.UnitDim SurfaceArea As NXOpen.Unit=CType(workPart.UnitCollection.FindObject("SquareMilliMeter"), NXOpen.Unit)Dim Volume As NXOpen.Unit=CType(workPart.UnitCollection.FindObject("CubicMilliMeter"), NXOpen.Unit)Dim Mass As NXOpen.Unit=CType(workPart.UnitCollection.FindObject("Kilogram"), NXOpen.Unit)Dim Length As NXOpen.Unit=CType(workPart.UnitCollection.FindObject("MilliMeter"), NXOpen.Unit)Dim Weight As NXOpen.Unit=CType(workPart.UnitCollection.FindObject("Newton"), NXOpen.Unit) Dim measureBodies1 As NXOpen.MeasureBodies=Nothing measureBodies1 = workPart.MeasureManager.NewMassProperties(massUnits1, 0.98999999999999999, False, measureBodyBuilder1.BodyCollector)Dim measure1 As NXOpen.Measure=Nothing measure1 = measureBodies1.CreateFeature() measureBodies1.Dispose() measureBodyBuilder1.Destroy() ' ----------------------------------------------' Menu: Tools-> Expression-> Rename Measured Body Expressions' ---------------------------------------------- ForEach temp As Expression In workPart.Expressions If temp.Units.Symbol="kg"Then workPart.Expressions.Rename(temp, "Mass")EndIf ' If temp.Units.Symbol = "mm²" Then' workPart.Expressions.Rename(temp, "SurfaceArea")' End If ' If temp.Units.Symbol = "mm³" Then' workPart.Expressions.Rename(temp, "Volume")' End If ' If temp.Units.Symbol = "N" Then' workPart.Expressions.Rename(temp, "Weight")' End If Next ' ----------------------------------------------' Dialog Begin Expressions Properties Builder' ---------------------------------------------- Dim objectsAttribute(0)As NXOpen.NXObject objectsAttribute(0)= workPart Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder=Nothing attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objectsAttribute, NXOpen.AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.Category="Materials" attributePropertiesBuilder1.Title="Weight" attributePropertiesBuilder1.DataType= NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.Number'attributePropertiesBuilder1.Units = "Tesla" attributePropertiesBuilder1.Units="Kilogram" Dim MassProperties As NXOpen.Expression=Nothing MassProperties = workPart.Expressions.CreateSystemExpressionWithUnits("0.0", Mass) MassProperties.RightHandSide="Mass" attributePropertiesBuilder1.IsReferenceType=False attributePropertiesBuilder1.Expression= MassProperties Dim nXObject2 As NXOpen.NXObject=Nothing nXObject2 = attributePropertiesBuilder1.Commit() attributePropertiesBuilder1.Destroy() EndSub Function SelectSolidToAssignMaterial(ByVal prompt AsString, ByRef selObj As TaggedObject)As Selection.Response Dim theUI As UI = UI.GetUIDim title AsString="Select Solid To Assign Material"Dim includeFeatures AsBoolean=FalseDim keepHighlighted AsBoolean=FalseDim selAction As Selection.SelectionAction= Selection.SelectionAction.ClearAndEnableSpecificDim cursor As Point3d Dim scope As Selection.SelectionScope= Selection.SelectionScope.WorkPartDim selectionMask_array(0)As Selection.MaskTriple With selectionMask_array(0).Type= UFConstants.UF_solid_type.SolidBodySubtype= UFConstants.UF_UI_SEL_FEATURE_SOLID_BODYEndWith Dim resp As Selection.Response= theUI.SelectionManager.SelectTaggedObject(prompt, _ title, scope, selAction, _ includeFeatures, keepHighlighted, selectionMask_array, _ selobj, cursor)If resp = Selection.Response.ObjectSelectedOrElse resp = Selection.Response.ObjectSelectedByNameThenReturn Selection.Response.OkElseReturn Selection.Response.CancelEndIf EndFunction PublicFunction GetUnloadOption(ByVal dummy AsString)AsInteger 'Unloads the image immediately after execution within NX GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately EndFunction 'Function DeleteMeasure() As NXOpen.Measure' Dim objects1(0) As NXOpen.TaggedObject' Dim MeasureToDelete As NXOpen.Measure = CType(workPart.Features.FindObject("BODY_MEASUREMENT(4)"), NXOpen.Measure)' objects1(0) = MeasureToDelete'End Function EndModule PublicClass AddMaterial Private _frmAttributeValue AsStringPublicProperty MaterialToAdd()AsStringGetReturn _frmAttributeValue EndGetSet(ByVal value AsString) _frmAttributeValue = value EndSetEndProperty Private _canceled AsBoolean=FalsePublicReadOnlyProperty Canceled()AsBooleanGetReturn _canceled EndGetEndProperty PrivateSub Form1_Load(sender As System.Object, e As System.EventArgs)HandlesMyBase.Load Label1.Text="Material" TextBox1.Text= _frmAttributeValue EndSub PrivateSub btnCancel_Click(sender As System.Object, e As System.EventArgs)Handles btnCancel.Click _canceled =TrueMe.Close()EndSub PrivateSub btnOK_Click(sender As System.Object, e As System.EventArgs)Handles btnOK.Click _frmAttributeValue = TextBox1.Text.ToUpperMe.Close()EndSub EndClass <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ PartialClass AddMaterial Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list.<System.Diagnostics.DebuggerNonUserCode()> _ ProtectedOverridesSub Dispose(ByVal disposing AsBoolean)TryIf disposing AndAlso components IsNotNothingThen components.Dispose()EndIfFinallyMyBase.Dispose(disposing)EndTryEndSub 'Required by the Windows Form DesignerPrivate components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor.<System.Diagnostics.DebuggerStepThrough()> _ PrivateSub InitializeComponent()Me.btnCancel=New System.Windows.Forms.Button()Me.btnOK=New System.Windows.Forms.Button()Me.Label1=New System.Windows.Forms.Label()Me.TextBox1=New System.Windows.Forms.TextBox()Me.SuspendLayout()''btnCancel'Me.btnCancel.DialogResult= System.Windows.Forms.DialogResult.CancelMe.btnCancel.Location=New System.Drawing.Point(178, 107)Me.btnCancel.Name="btnCancel"Me.btnCancel.Size=New System.Drawing.Size(85, 50)Me.btnCancel.TabIndex=0Me.btnCancel.Text="Cancel"Me.btnCancel.UseVisualStyleBackColor=True''btnOK'Me.btnOK.Location=New System.Drawing.Point(66, 107)Me.btnOK.Name="btnOK"Me.btnOK.Size=New System.Drawing.Size(85, 50)Me.btnOK.TabIndex=1Me.btnOK.Text="Ok"Me.btnOK.UseVisualStyleBackColor=True''Label1'Me.Label1.Location=New System.Drawing.Point(12, 54)Me.Label1.Name="Label1"Me.Label1.Size=New System.Drawing.Size(79, 13)Me.Label1.TabIndex=2Me.Label1.Text="Label1"Me.Label1.TextAlign= System.Drawing.ContentAlignment.MiddleRight''TextBox1'Me.TextBox1.Location=New System.Drawing.Point(97, 51)Me.TextBox1.Name="TextBox1"Me.TextBox1.Size=New System.Drawing.Size(166, 20)Me.TextBox1.TabIndex=3''MaterialCreation'Me.AcceptButton=Me.btnOKMe.AutoScaleDimensions=New System.Drawing.SizeF(6.0!, 13.0!)Me.AutoScaleMode= System.Windows.Forms.AutoScaleMode.FontMe.CancelButton=Me.btnCancelMe.ClientSize=New System.Drawing.Size(284, 176)Me.Controls.Add(Me.TextBox1)Me.Controls.Add(Me.Label1)Me.Controls.Add(Me.btnOK)Me.Controls.Add(Me.btnCancel)Me.FormBorderStyle= System.Windows.Forms.FormBorderStyle.FixedDialogMe.MaximizeBox=FalseMe.MinimizeBox=FalseMe.Name="AddMaterial"Me.Text="AddMaterial"Me.ResumeLayout(False)Me.PerformLayout() EndSubFriendWithEvents btnCancel As System.Windows.Forms.ButtonFriendWithEvents btnOK As System.Windows.Forms.ButtonFriendWithEvents Label1 As System.Windows.Forms.LabelFriendWithEvents TextBox1 As System.Windows.Forms.TextBoxEndClass <vbnet> Thanks