YZ Sketch Plane Sketch Coordinates not what was expected?

YZ Sketch Plane Sketch Coordinates not what was expected?

ebunn3
Advocate Advocate
1,242 Views
9 Replies
Message 1 of 10

YZ Sketch Plane Sketch Coordinates not what was expected?

ebunn3
Advocate
Advocate

Hi,

 

Please refer to the code and screenshots below.  I am doing the following:

  • Creating a sketch on the YZPlane
  • Projecting a body's outline on to the sketch.
  • Pulling the sketch bounding box coordinates.
  • Trying to plot a point at minPoint.
  • The point is going to the maxPoint coordinates which is backwards from what I expected.

Can anyone explain this to me?  

 

Thanks,

Eric

 

ebunn3_2-1628444440499.png

 

ebunn3_0-1628444374039.pngebunn3_1-1628444385299.png

 

 

#Author-Bunn
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        radius1 = 2
        radius2 = 0.156

        #get the application
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct

        # #get the root component
        # rootComp = design.rootComponent
        #Cast the root component (casting versus setting will get access to more intellisense)
        rootComp = adsk.fusion.Component.cast(design.rootComponent)

        #create a new sketch on the xy plane
        sketches = rootComp.sketches
        Plane = rootComp.yZConstructionPlane
        sketch = sketches.add(Plane)
        sketch.name = 'YZ Plane'

        #get the id of the cushion body
        prodBody = adsk.core.ObjectCollection.create()
        prodBody = rootComp.bRepBodies.itemByName('Pump')

        # #Create Base Line on Sketch
        # lines = sketch.sketchCurves.sketchLines
        # line1 = lines.addByTwoPoints(adsk.core.Point3D.create(maxY , minZ , 0), adsk.core.Point3D.create(minY , minZ ,0))
        # Get sketch points

        sketch.project(prodBody)
        BB = sketch.boundingBox
        minBB = BB.minPoint
        maxBB = BB.maxPoint
        
        minX = minBB.x
        minY = minBB.y
        minZ = minBB.z

        maxX = maxBB.x
        maxY = maxBB.y
        maxZ = maxBB.z

        sketchPoints = sketch.sketchPoints
        
        # Create sketch point
        point1 = adsk.core.Point3D.create(minX, minY, 0)
        sketchPoint1 = sketchPoints.add(point1)



    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

 

 

0 Likes
Accepted solutions (1)
1,243 Views
9 Replies
Replies (9)
Message 2 of 10

kandennti
Mentor
Mentor

Hi @ebunn3 .

 

If you use the modelToSketchSpace method, it will be drawn at the correct position.

・・・
        BB = sketch.boundingBox
        minBB = BB.minPoint
        maxBB = BB.maxPoint
        
        minBB = sketch.modelToSketchSpace(minBB)
        minX = minBB.x
        minY = minBB.y
        minZ = minBB.z
・・・

 

However, the BoundingBox property is generally inaccurate. Please refer to here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/non-minimum-profile-boundingbox-is-it-a-bu... 

 

If you want to get a BoundingBox with good accuracy, it is better to use the MeasureManager.getOrientedBoundingBox method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-DE730595-9235-4DA1-8096-22A058921819

 

0 Likes
Message 3 of 10

ebunn3
Advocate
Advocate

 @kandennti 

 

Thank you again.  I don’t need it to be that accurate as long as it fully encloses the geometry.  I’ll check out both methods.   

Thanks

 

Eric

Message 4 of 10

ebunn3
Advocate
Advocate

@kandennti 

 

Still having issues.  modeToSketchSpace does in fact transform the bounding box coordinates to sketch space as you stated.  I still have minZ going to maxZ?  I went into the sketch and it looks as though the Y axis is inverted with positive values going down?  Is this correct?  If so I need to use maxZ to get to the bottom left corner of the sketch.  I have Default Modeling Orientation set to ZUp in preferences.  Could this be part of the problem?  

 

Eric

 

ebunn3_0-1628516251562.png

 

0 Likes
Message 5 of 10

kandennti
Mentor
Mentor

@ebunn3 .

 

I'm sorry, I made a mistake.
Do not use the modeToSketchSpace method.

 

I think the reason why maxPoint and minPoint do not become the coordinate values of the desired position is because they are affected by the xDirection and yDirection of the sketch.

1.png

2.png

 

If you run the following script while editing a sketch, it will move the screen to match the xDirection and yDirection of the sketch.

 

 

# Fusion360API Python script
import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct

        skt: adsk.fusion.Sketch = adsk.fusion.Sketch.cast(des.activeEditObject)
        if not skt:
            return

        vp: adsk.core.Viewport = app.activeViewport
        camera: adsk.core.Camera = vp.camera

        target: adsk.core.Point3D = camera.eye.copy()
        targetVec: adsk.core.Vector3D = skt.xDirection.crossProduct(skt.yDirection)
        targetVec.scaleBy(-1)
        target.translateBy(targetVec)
        camera.target = target

        camera.upVector = skt.yDirection

        vp.camera = camera
        vp.fit()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

 

The point indicated by the mouse is the maxPoint.

 

It's hard to tell because it's different from the GUI sketch editing state.

0 Likes
Message 6 of 10

ebunn3
Advocate
Advocate

@kandennti 

 

So the sketch coordinate system is not the same as the world coordinate system apparently?   I’m trying to plot points using the world coordinate system and it does not match the viewport?   A little confusing.  So if I run the code you have attached it will match the viewport to the world coordinate system like old fashioned Autocad?


Not sure I really understand what is happening here?   I had similar issues in Solidworks with their coordinate system.  

Eric

0 Likes
Message 7 of 10

ebunn3
Advocate
Advocate

@kandennti 

 

Can one plot points in the sketch using a transform?   

Eric

0 Likes
Message 8 of 10

ebunn3
Advocate
Advocate

@kandennti 

 

I found this example for rotating sketch entities using transforms.  Is there a way to use this to add geometry directly to the sketch transforming the coordinates from World Coordinate System to sketch coordinate system?

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/rotating-sketch/m-p/7525161#M5000 

 

Eric

 

0 Likes
Message 9 of 10

kandennti
Mentor
Mentor
Accepted solution

@ebunn3 .

 

The origin of the sketch does not always coincide with the origin of the component.

Create a cylinder centered at a different location than the component origin, and create an offset plane on the top surface.
If you create a sketch based on this plane, the component origin and the sketch origin will be in different positions.

1.png

Green: Component origin
Blue: Origin of the sketch

 

Likewise, the positive x and y directions of the sketch seem to be different from the positive x and y directions of the component.

 

 

I have created the following test script.

# Fusion360API Python script

import traceback
import adsk.cam
import adsk.fusion
import adsk.core

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct

        root: adsk.fusion.Component = des.rootComponent
        body: adsk.fusion.BRepBody = root.bRepBodies.itemByName('Pump')

        planes = [
            root.xYConstructionPlane,
            root.yZConstructionPlane,
            root.xZConstructionPlane
        ]

        skts: adsk.fusion.Sketches = root.sketches

        plane: adsk.fusion.ConstructionPlane
        for plane in planes:

            # create sketch
            skt: adsk.fusion.Sketch = skts.add(plane)
            skt.name = plane.name

            # project
            skt.project(body)

            # bounding box
            bBox: adsk.core.BoundingBox3D = skt.boundingBox
            maxPnt: adsk.core.Point3D = bBox.maxPoint
            minPnt: adsk.core.Point3D = bBox.minPoint

            # ---------
            # Test
            # maxPnt = skt.modelToSketchSpace(maxPnt)
            # minPnt = skt.modelToSketchSpace(minPnt)
            # ---------

            # add point
            pnts: adsk.fusion.SketchPoints = skt.sketchPoints
            maxSktPnt: adsk.fusion.SketchPoint = pnts.add(maxPnt)
            minSktPnt: adsk.fusion.SketchPoint = pnts.add(minPnt)

            # create circle - max point only
            crvs: adsk.fusion.SketchCurves = skt.sketchCurves
            crvs.sketchCircles.addByCenterRadius(maxSktPnt.geometry, 0.5)

            # create rectangle
            crvs.sketchLines.addTwoPointRectangle(maxSktPnt, minSktPnt)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

It uses the same process with XY, YZ, and XZ to get the BoundingBox.
I used maxPoint and minPoint to draw a Rectangle, and maxPoint to create a circle for the marker.

 

Also, if you change the comments in the next two lines and run the program, you will see that the result is obviously wrong.

・・・
            # ---------
            # Test
            maxPnt = skt.modelToSketchSpace(maxPnt) # <- Here
            minPnt = skt.modelToSketchSpace(minPnt) # <- Here
            # ---------
・・・

 

From this, we can conclude that sketch.boundingBox is getting the return value as the coordinate value in the sketch.

 

0 Likes
Message 10 of 10

ebunn3
Advocate
Advocate

@kandennti 

 

Thank you again!!  This is a great example to not only see how the bounding box coordinates match up with the sketch coordinates.  I can work with this and as long is Fusion is consistent this should work for me.  I did notice that if you manually create a sketch by clicking on the YZ plane for example the X and Y directions are what you would expect them to be.  Not the case if you click on the XZ Axis where the Y axis is inverted.  Using the API gives you completely different results.  As I said as long as they are consistent I can work with this.  

 

Thanks again,

 

Eric

0 Likes