Cancel
Showing results for 
Search instead for 
Did you mean: 

Bounding box in Python

Genius
Genius

Hi there, I'm trying to create a bounding box for solid bodies in Python.

 

I'm sure there is a straightforward to do it in C++  but I prefer to work in python. Below is the documentation link and my code

 

https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_pyt...

 

def boundingbox():

# Get Session
NXSession = NXOpen.Session.GetSession()
# Get Work Part

workPart = NXSession.Parts.Work
# Get Displayed Part

displayPart = NXSession.Parts.Display

nullFeature = NXOpen.Features.Feature.Null
#origin = Point3d(0.0, 0.0, 0.0)
def processPart(partObject):
for bodyObject in partObject.Bodies:
processBodyFaces(bodyObject)
processBodyEdges(bodyObject)
##access all parts in an nx session
for partObject in NXSession.Parts:
global my_part
my_part=processPart(partObject)

my_part.AskBoundingBox()

 

 

13 REPLIES

Re: Bounding box in Python

Siemens Phenom Siemens Phenom
Siemens Phenom

You need to get the UF session, then access ModlGeneral.AskBoundingBoxExact() from there.

 

This is just "best-guess" code - I haven't tried making yours work so that I could add the reply in:

 

import NXOpen.UF

def boundingbox():

    # Get Session
    NXSession = NXOpen.Session.GetSession()
    theUfSession = NXOpen.UF.UFSession.GetUFSession()

    # Get Work Part
    workPart = NXSession.Parts.Work

    # Get Displayed Part
    displayPart = NXSession.Parts.Display

    nullFeature = NXOpen.Features.Feature.Null
    #origin = Point3d(0.0, 0.0, 0.0)
    def processPart(partObject):
        for bodyObject in partObject.Bodies:
            processBodyFaces(bodyObject)
            processBodyEdges(bodyObject)
    ##access all parts in an nx session
    for partObject in NXSession.Parts:
        global my_part
        my_part=processPart(partObject)

    (min_corner,directions,distances) = theUfSession.ModlGeneral.AskBoundingBoxExact(my_part.Tag, NULL_TAG)

    

 

Re: Bounding box in Python

Genius
Genius

Thanks for the reply. I've imported the Uf library and now I am trying various syntax.

 

def boundingbox():

# Get Session
NXSession = NXOpen.Session.GetSession()

# Get UF session
theUfSession = NXOpen.UF.UFSession.GetUFSession()

# Get Work Part
workPart = NXSession.Parts.Work
#workpart = NXOpen.session.Parts.Work

# Get Displayed Part
displayPart = NXSession.Parts.Display

#nullFeature = NXOpen.Features.Feature.Null
#origin = Point3d(0.0, 0.0, 0.0)


##access all solid bodies in a part
#def processPart(partObject):
# for bodyObject in partObject.Bodies:
# processBodyFaces(bodyObject)
# processBodyEdges(bodyObject)


##access all parts in an nx session
for partObject in NXSession.Parts:
global my_part
my_part=processPart(partObject)



boundedbox=NXOpen.UF.ModlGeneral

boundedbox.AskBoundingBox(displayPart)

Re: Bounding box in Python

Siemens Phenom Siemens Phenom
Siemens Phenom

I showed the correct syntax of the call in my original reply. Here is fully working code that will list the bounding box for each body with respect to the WCS:

 

import NXOpen
import NXOpen.UF

theSession = NXOpen.Session.GetSession()
theLw = theSession.ListingWindow
theUI = NXOpen.UI.GetUI()
theUfSession = NXOpen.UF.UFSession.GetUFSession()

def main(*args): 

    workPart = theSession.Parts.BaseWork

    bodies = workPart.Bodies

    for body in bodies:

        (min_corner, directions, distances) = theUfSession.ModlGeneral.AskBoundingBoxExact(body.Tag, 0)

        theLw.Open()
        theLw.WriteLine("Bounding box for " + body.JournalIdentifier)

        theLw.WriteLine("  Min Corner: ")
        theLw.WriteLine("    X: " + str(min_corner[0]))
        theLw.WriteLine("    Y: " + str(min_corner[1]))
        theLw.WriteLine("    Z: " + str(min_corner[2]))

        theLw.WriteLine("  Direction Vectors: ")
        theLw.WriteLine("    X: (" + str(directions[0][0]) + ", " + str(directions[0][1]) + ", " + str(directions[0][2]) + ")")
        theLw.WriteLine("    Y: (" + str(directions[1][0]) + ", " + str(directions[1][1]) + ", " + str(directions[1][2]) + ")")
        theLw.WriteLine("    Z: (" + str(directions[2][0]) + ", " + str(directions[2][1]) + ", " + str(directions[2][2]) + ")")

        theLw.WriteLine("  Distances: ")
        theLw.WriteLine("    X: " + str(distances[0]))
        theLw.WriteLine("    Y: " + str(distances[1]))
        theLw.WriteLine("    Z: " + str(distances[2]))

        theLw.WriteLine("")

# Main program.

if __name__ == "__main__":
    main()

 

Re: Bounding box in Python

Genius
Genius
Thanks, that worked. is there a way to draw a bounding box with Feature Builder?

Re: Bounding box in Python

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Do you need the bounding box as temporary display or as solid?

 

Feature builder can't do it.

 

You would use temporary lines or a block.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.1, FBM, MRL 3.1.7 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 11.4
Development: C (ITK), .NET, Tcl/Tk Testing: NX12.0 | AWC 3.4 Preparing: NX12.0

Employees of the customers, together we are strong Smiley Wink
How to Get the Most from Your Signature in the Community
NX Customization - Best Practice Guide

Re: Bounding box in Python

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@Stefan_Pendl wrote:

Do you need the bounding box as temporary display or as solid?

 

Feature builder can't do it.

 

You would use temporary lines or a block.


Depends on the version of NX in use. NX 10 has added the "create box" command to the modeling application which will create an associative bounding box around the selected geometry.

 

I'd suggest recording a journal while using the "create box" command to see what code gets recorded.

Re: Bounding box in Python

Genius
Genius

Thanks, the create box command did the trick

 

## create bounding box

toolingBoxBuilder1 = workPart.Features.ToolingFeatureCollection.CreateToolingBoxBuilder(NXOpen.Features.ToolingBox.Null)

#create instance of a class
matrix1 = NXOpen.Matrix3x3()
#attributes
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

global xorigin, yorigin, zorigin
xorigin=min_corner[0]+(distances[0]/2)
yorigin=min_corner[1]+(distances[1]/2)
zorigin=min_corner[2]+(distances[2]/2)

#assigning a method to variable
position1 = NXOpen.Point3d(xorigin, yorigin, zorigin)

toolingBoxBuilder1.SetBoxMatrixAndPosition(matrix1, position1)

toolingBoxBuilder1.CalculateBoxSize()

toolingBoxBuilder1.XValue.RightHandSide = str(distances[0])

toolingBoxBuilder1.CalculateBoxSize()

toolingBoxBuilder1.YValue.RightHandSide = str(distances[1])

toolingBoxBuilder1.CalculateBoxSize()

toolingBoxBuilder1.ZValue.RightHandSide = str(distances[2])

toolingBoxBuilder1.CalculateBoxSize()

nXObject1 = toolingBoxBuilder1.Commit()

toolingBoxBuilder1.Destroy()

Re: Bounding box in Python

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

The CreateToolingBoxBuilder is usually licensed with one of the tooling wizard licenses, like Mold Wizard, Electrode Design, etc.

 

I don't know, if this is now included in the modeling license starting with NX 10.

 

Just to make others aware of the problem, that it might not work in all circumstances.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.1, FBM, MRL 3.1.7 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 11.4
Development: C (ITK), .NET, Tcl/Tk Testing: NX12.0 | AWC 3.4 Preparing: NX12.0

Employees of the customers, together we are strong Smiley Wink
How to Get the Most from Your Signature in the Community
NX Customization - Best Practice Guide

Re: Bounding box in Python

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor