Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issue Generating the Sketch for a face with Dimensions using Python

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
sidharth_bab
251 Views, 4 Replies

Issue Generating the Sketch for a face with Dimensions using Python

Hi everyone,

 

I am trying to generate a sketch of a face from the STP file of a part. Obviously, the STP file created on Inventor doesn't bring any design history to Fusion. Since my employees are using Fusion to take measurements, its time consuming for them to use "Inspect" feature.

So I managed to create a python to do the work. What I expect to do is the program once ran, should ask me to select a Face, and once the face is selected, it will generate a plane on that Face (I used Offset Plane with value 0) and a sketch is generated on that Face of the Face with Dimensions. 
However, how hard I tried, I can only generate the sketch on XY plane even though the face is on some other plane. I even tried Projecting the geometry, but its having difficulties dimensioning.

 

Can anyone help me to figure out what's happening here. I will attach the code below. Since I know only the basics of Python, I used ChatGPT for this. If anyone knows any alternative or better approach or a python code that helps to get the dimensions of a 3D STP file with no design history, that would be great!!!

 

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

def create_offset_plane(selected_face, root_comp):
    try:
        # Create a construction plane at 0 offset from the selected face
        plane_input = root_comp.constructionPlanes.createInput()
        plane_input.setByOffset(selected_face, adsk.core.ValueInput.createByReal(0))
        return root_comp.constructionPlanes.add(plane_input)
    
    except Exception as e:
        if ui:
            ui.messageBox('Failed to create offset plane:\n{}'.format(traceback.format_exc()))
        return None

def add_dimensions_to_sketch(sketch, selected_face):
    try:
        # Add edges to the sketch and dimensions
        for edge in selected_face.edges:
            startPoint = edge.startVertex.geometry
            endPoint = edge.endVertex.geometry
            sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)

            # Add a dimension to the line
            sketch.sketchDimensions.addDistanceDimension(
                sketchLine.startSketchPoint,
                sketchLine.endSketchPoint,
                adsk.fusion.DimensionOrientations.AlignedDimensionOrientation,
                adsk.core.Point3D.create(
                    (startPoint.x + endPoint.x) / 2,
                    (startPoint.y + endPoint.y) / 2,
                    (startPoint.z + endPoint.z) / 2
                )
            )

    except Exception as e:
        if ui:
            ui.messageBox('Failed to add dimensions to sketch:\n{}'.format(traceback.format_exc()))

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        design = app.activeProduct

        # Get the root component of the active design
        root_comp = design.rootComponent

        # Get all selected entities (faces) from the user's selection
        selected_entities = ui.activeSelections
        selected_faces = []
        
        # Filter and collect selected faces
        for selection in selected_entities:
            if isinstance(selection.entity, adsk.fusion.BRepFace):
                selected_faces.append(selection.entity)
        
        # Check if faces are selected
        if selected_faces:
            # Create a new offset plane at 0 offset from the selected face
            offset_plane = create_offset_plane(selected_faces[0], root_comp)
            
            if offset_plane:
                # Create a new sketch on the offset plane
                sketches = root_comp.sketches
                sketch = sketches.add(offset_plane)

                # Add dimensions to the sketch based on the selected face
                add_dimensions_to_sketch(sketch, selected_faces[0])

                # Exit the script cleanly
                adsk.terminate()
            
    except Exception as e:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Entry point of Fusion 360 script
if __name__ == '__main__':
    import adsk.fusion
    import traceback

    # Start Fusion 360 and run the main function
    app = adsk.core.Application.get()
    ui = app.userInterface
    if ui:
        # Prompt the user to run the script
        ui.messageBox('To start, run the `run` function.')

 

 

 

Screenshot 1.jpg

Screenshot 2.jpg

  

 

 

 

 

Labels (3)
4 REPLIES 4
Message 2 of 5
kandennti
in reply to: sidharth_bab

Hi @sidharth_bab -San.

 

I think the correct behavior would be to use the modelToSketchSpace method to find the coordinates in the sketch.

・・・
def add_dimensions_to_sketch(sketch: adsk.fusion.Sketch, selected_face):
    try:
        # Add edges to the sketch and dimensions
        for edge in selected_face.edges:
            startPoint = edge.startVertex.geometry
            endPoint = edge.endVertex.geometry
            # sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)
            sktStartPoint: adsk.core.Point3D = sketch.modelToSketchSpace(startPoint)
            sktEndPoint: adsk.core.Point3D = sketch.modelToSketchSpace(endPoint)
            sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(
                sktStartPoint, 
                sktEndPoint
            )
            sketch.areDimensionsShown = True
・・・
Message 3 of 5
sidharth_bab
in reply to: kandennti

That did fix the issue of having the sketch on the constructed plane!! thanks a lot!! Not sure if you can look into this too. I used the code you provided and replaced 'def add_dimensions_to_sketch', the thing is its not giving any dimensions and also its neglecting the circles or any curved geometry by drawing a straight line. I am looking to see if the code can sketch out the holes and curves so that I can get the radius and the distance of the holes comes up when running the code

 

sidharth_bab_0-1718895288457.png

 

Message 4 of 5
kandennti
in reply to: sidharth_bab

@sidharth_bab -San.

 

I omitted too much.

・・・
def add_dimensions_to_sketch(sketch: adsk.fusion.Sketch, selected_face):
    try:
        # Add edges to the sketch and dimensions
        for edge in selected_face.edges:
            startPoint = edge.startVertex.geometry
            endPoint = edge.endVertex.geometry
            # sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)
            sktStartPoint: adsk.core.Point3D = sketch.modelToSketchSpace(startPoint)
            sktEndPoint: adsk.core.Point3D = sketch.modelToSketchSpace(endPoint)
            sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(
                sktStartPoint, 
                sktEndPoint
            )
            sketch.areDimensionsShown = True

            # Add a dimension to the line
            sketch.sketchDimensions.addDistanceDimension(
                sketchLine.startSketchPoint,
                sketchLine.endSketchPoint,
                adsk.fusion.DimensionOrientations.AlignedDimensionOrientation,
                adsk.core.Point3D.create(
                    (startPoint.x + endPoint.x) / 2,
                    (startPoint.y + endPoint.y) / 2,
                    (startPoint.z + endPoint.z) / 2
                )
            )
・・・
Message 5 of 5
sidharthbabu8
in reply to: kandennti

Really do appreciate it. This worked and was able to use as my base to start. I am advancing forward but still facing more issues which I posted on:
Issue with Organizing Dimensioning using Python  . If you are interested, can you have peek and suggest me some solutions. Thank you!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report