bounding Box intersection issue

bounding Box intersection issue

Joshua.mursic
Advocate Advocate
735 Views
4 Replies
Message 1 of 5

bounding Box intersection issue

Joshua.mursic
Advocate
Advocate

I am having a strange problem where ui.messageBox() is actually causing my script to function correctly. And without it the script is broken.

 

I am importing in meshbodies and building a bounding box around them. Then I copy the bounding box and move it around each occurrence in the project. At each interval of movement I check to see if the copied bounding box is intersecting with any other bounding box in the project (excluding the mesh that was just imported), and if it is, I keep moving the part around each occurrence until it can find a spot. Image shows the path of the part around an occurrence.

 

Joshuamursic_0-1680291553812.png

 

This is working great. However the problem happens when I try to use this function in a loop and import 2 projects in one after another. When I do this, the first part imports fine, but when the second part uses the Intersection checker, it is not registering intersection with the first imported part. The strange thing is that when I use ui.messageBox() to display the result of the intersection check, the 2nd part imports correctly. I tried to use a sleep() in my program but that is not the issue. Any ideas?

 

expected result:

Joshuamursic_1-1680291943427.png

 

current result: (importing on top of 1st part)

Joshuamursic_2-1680291983992.png

 

def isIntersecting(selectionBBox:adsk.core.BoundingBox3D):
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    root = design.rootComponent
    occCount = root.occurrences.count

    for i in range(occCount-1):
        occ = root.occurrences.item(i)
        occBBox = occ.boundingBox
        if occBBox.intersects(selectionBBox):
            return True

    return False

 

 

0 Likes
Accepted solutions (1)
736 Views
4 Replies
Replies (4)
Message 2 of 5

Joshua.mursic
Advocate
Advocate

The moveObj() function I am using is running before I even import the next mesh, but for some reason when I calculate the bounding boxes, it is using the original import location of the first part.

 

here is an image of the first loop, you can see it finds a mesh body that is already in the project and builds a bounding box around it. 

Joshuamursic_0-1680294256515.png

 

Then It moves to that part, and then the next part is inserted and the bounding boxes are recalculated. But as you can see the bounding box for the part that was inserted previously is being constructed from where it originally imported, not where it was moved to.

Joshuamursic_1-1680294342480.png

 

because of this the second part is inserted directly on top of the first part that is inserted.

Joshuamursic_2-1680294457971.png

 

If this a problem with fusion calculating the bounding boxes faster then the object transform can move them? But if that is the case, my functions are still being called in a sequence and so the boundary intersection check should not start until after the object has moved.

 

 

Here section of code that is moving the mesh around the other meshes looking for spots.

 # Move in -Y
            while loopBoxMax.y >= compMinPoint.y:
                loopBoxMin = adsk.core.Point3D.create(compMaxPoint.x+xIncrement+padding,compMaxPoint.y+yIncrement,(maxPoint.z - minPoint.z)/-2)
                loopBoxMax = adsk.core.Point3D.create(loopBoxMin.x+meshDimensions.x,loopBoxMin.y+meshDimensions.y,loopBoxMin.z+meshDimensions.z)
                loopBox = adsk.core.BoundingBox3D.create(loopBoxMin,loopBoxMax)
                if insideStock(loopBox,stock,"circle") and intersection(loopBox) == False:
                    ui.messageBox(f'Positioning relative too {comp.name}')
                    moveVector = adsk.core.Vector3D.create(loopBox.maxPoint.x-maxPoint.x,loopBox.maxPoint.y-maxPoint.y,loopBox.maxPoint.z-maxPoint.z)
                    moveObj(mesh,moveVector)
                    return  True
                yIncrement -= incrementValue

 

and here is my moveObj() function

def moveObj(mesh:adsk.fusion.MeshBody,vector:adsk.core.Vector3D):
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    root = design.rootComponent
    features = root.features

    rootOccCount = root.occurrences.count
    meshAssembly = mesh.createForAssemblyContext(root.occurrences.item(rootOccCount-1))

    meshCollection = adsk.core.ObjectCollection.create()
    meshCollection.add(meshAssembly)
    
    # Create Move Inputs
    x = adsk.core.ValueInput.createByReal(vector.x)
    y = adsk.core.ValueInput.createByReal(vector.y)
    z = adsk.core.ValueInput.createByReal(vector.z)

    # Excecute Move
    moveFeature = features.moveFeatures
    moveFeatureInput = moveFeature.createInput2(meshCollection)
    moveFeatureInput.defineAsTranslateXYZ(x,y,z,True)
    moveFeature.add(moveFeatureInput)

 

0 Likes
Message 3 of 5

Jorge_Jaramillo
Collaborator
Collaborator

Hi @Joshua.mursic ,

Could you share you code where you import the part to the model and the sequence until you invoque the moveObj()?

0 Likes
Message 4 of 5

MichaelT_123
Advisor
Advisor
Accepted solution

Hi Mr JoshuaMursic,

 

Consider looking at Snapshot Object.

Try including the following in your code:

 

    if  snapshots.hasPendingSnapshot:
        new_snap = snapshots.add()
 
Regards
MichaelT
MichaelT
Message 5 of 5

Joshua.mursic
Advocate
Advocate

Thanks this was the issue, the design environment was not capturing the position of the components after I moved them. Thank you very much.

0 Likes