'####################################################################
' Journal para calcular la caja mínima de las piezas de un ensamble.
'Crea atributos con los valores de la caja mínima.
'Limitado a un solo cuerpo por pieza.
'Código para recorrer el assembly obtenida de NXJournaling.com February 24, 2012
'Código para crear Csys a partir de una cara obtenido de GTAC: CreateCsysfromPlanarFace
'21.10.2020 Ampliado a varios cuerpos
'21.10.2020 Eliminado atributo CUERPOBRUTO
'21.10.2020 Corregido problema cuando hay más elementos además del cuerpo.
'21.10.2020 Las medidas se ordenan de mayor a menor (X,Y,Z)
' Creado por Javier Axpe
'####################################################################
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.Features
Imports System.Collections.Generic
Imports NXOpen.Utilities
Imports NXOpenUI
Module NXJournal
Public theSession As Session = Session.GetSession()
Public theUISession As UI = UI.GetUI
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow
Dim workPart As Part = theSession.Parts.Work
Dim theUfSession = UFSession.GetUFSession()
Dim dispPart As Part = theSession.Parts.Display
Dim origDisplayPart As Part = theSession.Parts.Display
Dim origWorkComp As NXOpen.Assemblies.Component = theSession.Parts.WorkComponent
Dim theUI As UI = UI.GetUI()
Dim listName As New List(Of String)
Sub Main()
'lw.Open()
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
'to process the work part rather than the display part,
' comment the previous line and uncomment the following line
'Dim c As ComponentAssembly = workPart.ComponentAssembly
If Not IsNothing(c.RootComponent) Then
'*** insert code to process 'root component' (assembly file)
'lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
'lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
'*** end of code to process root component
reportComponentChildren(c.RootComponent, 0)
Else
'*** insert code to process piece part
lw.WriteLine("Part has no components")
End If
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
'lw.Close()
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(origWorkComp, NXOpen.PartCollection.RefsetOption.Entire, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)
theUISession.NXMessageBox.Show("Material mínimo", NXMessageBox.DialogType.Information, "Cálculo de caja mínima (medidas Bruto) y volumen de pieza realizado con éxito.")
End Sub
'**********************************************************
Sub reportComponentChildren(ByVal comp As Component,
ByVal indent As Integer)
For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly
'lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
'*** end of code to process component or subassembly
If child.GetChildren.Length <> 0 Then
'*** this is a subassembly, add code specific to subassemblies
'lw.WriteLine(New String(" ", indent * 2) &
' "* subassembly with " &
' child.GetChildren.Length & " components")
'lw.WriteLine(New String(" ", indent * 2) &
' " + Active Arrangement: " &
' child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
'*** end of code to process subassembly
Else
'this component has no children (it is a leaf node)
'add any code specific to bottom level components
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(child, NXOpen.PartCollection.RefsetOption.Entire, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)
'Try
' Dim marcaProcesado = theSession.Parts.Work.GetStringAttribute("CUERPOBRUTO")
'Catch
Dim strFindString = child.Name
If listName.Contains(strFindString) Then
Else
listName.Add(child.Name)
Try
Dim Cuerpos() As Body
Cuerpos = AskAllBodies(theSession.Parts.Work)
CreateBoundedBox(Cuerpos)
theSession.Parts.SetWorkComponent(child, NXOpen.PartCollection.RefsetOption.Entire, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus1)
Dim actualWorkPart As NXOpen.Part = theSession.Parts.Work
theSession.Parts.SetDisplay(actualWorkPart, False, False, partLoadStatus1)
Dim objs() As NXObject = theSession.Parts.Work.Layers.GetAllObjectsOnLayer(2)
Dim count As Integer = theSession.UpdateManager.AddToDeleteList(objs)
Dim undoMark As Session.UndoMarkId = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Undo Mark")
theSession.UpdateManager.DoUpdate(undoMark)
theSession.Parts.SetDisplay(dispPart, False, True, partLoadStatus1)
Catch
End Try
'End Try
End If
End If
reportComponentChildren(child, indent + 1)
Next
End Sub
'**********************************************************
'**********************************************************
Function AskAllBodies(ByVal thePart As Part) As Body()
Dim theBodies As New System.Collections.ArrayList()
Dim aBodyTag As Tag = Tag.Null
Do
ufs.Obj.CycleObjsInPart(thePart.Tag,
UFConstants.UF_solid_type, aBodyTag)
If aBodyTag = Tag.Null Then
Exit Do
End If
Dim theType As Integer, theSubtype As Integer
ufs.Obj.AskTypeAndSubtype(aBodyTag, theType, theSubtype)
If theSubtype = UFConstants.UF_solid_body_subtype Then
theBodies.Add(theSession.GetObjectManager.GetTaggedObject(aBodyTag))
End If
Loop While True
Return DirectCast(theBodies.ToArray(GetType(Body)), Body())
End Function
Function CreateBoundedBox(ByVal cuerpos() As Body)
Dim tempWorkPart As Part = theSession.Parts.Work
Dim nullNXOpen_Features_ToolingBox As NXOpen.Features.ToolingBox = Nothing
Dim toolingBoxBuilder1 As NXOpen.Features.ToolingBoxBuilder = Nothing
toolingBoxBuilder1 = tempWorkPart.Features.ToolingFeatureCollection.CreateToolingBoxBuilder(nullNXOpen_Features_ToolingBox)
toolingBoxBuilder1.Type = NXOpen.Features.ToolingBoxBuilder.Types.BoundedBlock
toolingBoxBuilder1.NonAlignedMinimumBox = True
Dim matrix1 As NXOpen.Matrix3x3 = Nothing
matrix1.Xx = 1.0
matrix1.Xy = 0.0
matrix1.Xz = 0.0
matrix1.Yx = 0.0
matrix1.Yy = 1.0
matrix1.Yz = 0.0
matrix1.Zx = 0.0
matrix1.Zy = 0.0
matrix1.Zz = 1.0
Dim position1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
toolingBoxBuilder1.SetBoxMatrixAndPosition(matrix1, position1)
Dim n As Integer
n = cuerpos.Length
Dim bodies1(n) As NXOpen.Body
Dim body1() As NXOpen.Body = cuerpos
bodies1 = cuerpos
Dim bodyDumbRule1 As NXOpen.BodyDumbRule = Nothing
bodyDumbRule1 = tempWorkPart.ScRuleFactory.CreateRuleBodyDumb(bodies1, True)
Dim scCollector1 As NXOpen.ScCollector = Nothing
scCollector1 = toolingBoxBuilder1.BoundedObject
Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = bodyDumbRule1
scCollector1.ReplaceRules(rules1, False)
Dim selections1(n) As NXOpen.NXObject
selections1 = bodies1
Dim deselections1(-1) As NXOpen.NXObject
toolingBoxBuilder1.SetSelectedOccurrences(selections1, deselections1)
Dim selectNXObjectList1 As NXOpen.SelectNXObjectList = Nothing
selectNXObjectList1 = toolingBoxBuilder1.FacetBodies
Dim objects1(-1) As NXOpen.NXObject
Dim added1 As Boolean = Nothing
added1 = selectNXObjectList1.Add(objects1)
toolingBoxBuilder1.CalculateBoxSize()
Dim csysorigin1 As NXOpen.Point3d = New NXOpen.Point3d(50.0, 50.0, 50.0)
toolingBoxBuilder1.BoxPosition = csysorigin1
Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = toolingBoxBuilder1.Commit()
nXObject1.SetName("CAJA_MINIMA")
toolingBoxBuilder1.Destroy()
Dim objects2(0) As NXOpen.NXObject
objects2(0) = tempWorkPart
Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(tempWorkPart, objects2, NXOpen.AttributePropertiesBuilder.OperationType.None)
Dim featArray() As Features.Feature = theSession.Parts.Work.Features.GetFeatures()
Dim featEntities() As NXObject
For Each myFeature As Features.Feature In featArray
Dim tipo As String
Dim myBodies() As Body
Dim myFaces() As Face
tipo = myFeature.FeatureType.ToString
If myFeature.FeatureType.ToString = "Tooling Box" Then
Dim myBodyFeat As Features.BodyFeature = myFeature
myBodies = myBodyFeat.GetBodies()
myFaces = myBodies(0).GetFaces()
Dim face As Face = myFaces(0)
Dim matrixTag As Tag = Tag.Null
Dim cSysTag As Tag = Tag.Null
Dim faceType As Integer = 0
Dim normalDirection As Integer = 0
Dim origin As Double() = New Double(2) {}
Dim dir As Double() = New Double(2) {}
Dim boundedBox As Double() = New Double(5) {}
Dim radius As Double = 0
Dim radiusData As Double = 0
theUfSession.Csys.AskMatrixOfObject(face.Tag, matrixTag)
theUfSession.Modl.AskFaceData(face.Tag, faceType, origin, dir, boundedBox, radius, radiusData, normalDirection)
theUfSession.Csys.CreateTempCsys(origin, matrixTag, cSysTag)
Dim min_corner(2) As Double
Dim directions(2, 2) As Double
Dim distances(2) As Double
Dim edge_len(2) As String
ufs.Modl.AskBoundingBoxExact(myBodies(0).Tag, cSysTag, min_corner, directions,
distances)
Dim theObject(0) As Body
theObject(0) = myBodies(0)
Dim myMeasure As MeasureManager = theSession.Parts.Display.MeasureManager()
Dim massUnits(4) As Unit
massUnits(0) = theSession.Parts.Display.UnitCollection.GetBase("Area")
massUnits(1) = theSession.Parts.Display.UnitCollection.GetBase("Volume")
massUnits(2) = theSession.Parts.Display.UnitCollection.GetBase("Mass")
massUnits(3) = theSession.Parts.Display.UnitCollection.GetBase("Length")
Dim mb As MeasureBodies = Nothing
mb = myMeasure.NewMassProperties(massUnits, 0.99, theObject)
Dim objectArray1(0) As NXOpen.DisplayableObject
Dim body2 As NXOpen.Body = myBodies(0)
objectArray1(0) = body2
tempWorkPart.Layers.ApplyMoveToLayerToOwningParts(2, objectArray1)
objectArray1(0).Highlight()
objectArray1(0).Blank()
Dim nXObject2 As NXOpen.NXObject
Dim listMedidas As New List(Of Integer)
listMedidas.Add(CInt(distances(0)))
listMedidas.Add(CInt(distances(1)))
listMedidas.Add(CInt(distances(2)))
listMedidas.Sort(Function(valueA, valueB) valueB.CompareTo(valueA))
attributePropertiesBuilder1.Category = "BRUTO"
attributePropertiesBuilder1.Title = "BRUTO_X"
attributePropertiesBuilder1.StringValue = listMedidas(0).ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Category = "BRUTO"
attributePropertiesBuilder1.Title = "BRUTO_Y"
attributePropertiesBuilder1.StringValue = listMedidas(1).ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Category = "BRUTO"
attributePropertiesBuilder1.Title = "BRUTO_Z"
attributePropertiesBuilder1.StringValue = listMedidas(2).ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Category = "BRUTO"
attributePropertiesBuilder1.Title = "MEDIDAS_BRUTO"
attributePropertiesBuilder1.StringValue = listMedidas(0).ToString & " x " & listMedidas(1).ToString & " x " & listMedidas(2).ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Title = "VOLUMEN"
attributePropertiesBuilder1.StringValue = CInt(mb.Volume).ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Title = "PESO"
attributePropertiesBuilder1.StringValue = mb.Weight.ToString
nXObject2 = attributePropertiesBuilder1.Commit()
attributePropertiesBuilder1.Destroy()
End If
Next
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