Issue with Dimensioning using Python

Issue with Dimensioning using Python

sidharthbabu8
Explorer Explorer
353 Views
0 Replies
Message 1 of 1

Issue with Dimensioning using Python

sidharthbabu8
Explorer
Explorer

Hey Everyone,

Hope you guys are doing great!

 

I am trying to generate dimensions of a STP file which have no design history which is partially solved with the help of a community member: @kandennti  under the post  Issue Generating the Sketch for a face with Dimensions using Python 

 

So, my current situation is my python is able to sketch out all the geometry on the Face that I preselect. I brining in all the wanted and unwanted data/dimensions. Also these dimensions are overlapping. I will post the python code below. What I am trying to achieve is

  • keep the dimensions organized, make the non radial dimensions bring outside the sketch.
  • keep at least a min of 15mm distance between dimensions.
  • avoid overlapping
  • currently python gives the vertical distance from the edge (horizontal distances are from nearest points) , however due to some reason, the dimensions are from the furthest edge possible. I am thinking about getting the dimensions from the closest corner of the sketch so its practical for operators to check the position of holes and other features
  • Also, if the part is inclined, the program is not able to sketch any circles due to which I am not getting any hole dimensions

sidharthbabu8_0-1719248769282.png

This is my current situation. What you see here is on the STP file. 

I don't have much knowledge in Python due to which I use ChatGPT along with what I know. Please find the code below:

 

 

 

import adsk.core
import adsk.fusion
import adsk.cam
import 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:
        return None

def is_duplicate_dimension(existing_dimensions, point_one, point_two):
    for dimension in existing_dimensions:
        if dimension.objectType == adsk.fusion.SketchDimension.classType():
            if dimension.parameter.isDriven:
                continue
            if (dimension.startPoint.geometry.isEqualTo(point_one) and
                dimension.endPoint.geometry.isEqualTo(point_two)) or (
                dimension.startPoint.geometry.isEqualTo(point_two) and
                dimension.endPoint.geometry.isEqualTo(point_one)):
                return True
    return False

def add_dimensions_to_sketch(sketch: adsk.fusion.Sketch, selected_face):
    try:
        # Get the existing dimensions in the sketch
        existing_dimensions = sketch.sketchDimensions

        # Iterate over each edge of the selected face
        for edge in selected_face.edges:
            geometry = edge.geometry
            if geometry.curveType == adsk.core.Curve3DTypes.Line3DCurveType:
                # Handle line edges
                startPoint = edge.startVertex.geometry
                endPoint = edge.endVertex.geometry

                sktStartPoint = sketch.modelToSketchSpace(startPoint)
                sktEndPoint = sketch.modelToSketchSpace(endPoint)

                sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(
                    sktStartPoint,
                    sktEndPoint
                )
                sketch.areDimensionsShown = True

                midPoint = adsk.core.Point3D.create(
                    (startPoint.x + endPoint.x) / 2,
                    (startPoint.y + endPoint.y) / 2,
                    (startPoint.z + endPoint.z) / 2
                )

                try:
                    if not is_duplicate_dimension(existing_dimensions, sktStartPoint, sktEndPoint):
                        sketch.sketchDimensions.addDistanceDimension(
                            sketchLine.startSketchPoint,
                            sketchLine.endSketchPoint,
                            adsk.fusion.DimensionOrientations.AlignedDimensionOrientation,
                            midPoint
                        )
                except RuntimeError as e:
                    pass  # Suppress the error message related to over-constraint

            elif geometry.curveType == adsk.core.Curve3DTypes.Circle3DCurveType:
                # Handle circular edges
                circle = edge.geometry
                centerPoint = sketch.modelToSketchSpace(circle.center)
                radius = circle.radius

                sketchCircle = sketch.sketchCurves.sketchCircles.addByCenterRadius(
                    centerPoint,
                    radius
                )
                sketch.areDimensionsShown = True

                # Find the furthest point from the circle's center
                furthest_point = None
                max_distance = -1.0

                for sketchLine in sketch.sketchCurves.sketchLines:
                    for sketchPoint in [sketchLine.startSketchPoint, sketchLine.endSketchPoint]:
                        distance = centerPoint.distanceTo(sketchPoint.geometry)
                        if distance > max_distance:
                            max_distance = distance
                            furthest_point = sketchPoint

                if furthest_point:
                    # Determine orientation based on relative positions
                    if abs(furthest_point.geometry.x - centerPoint.x) > abs(furthest_point.geometry.y - centerPoint.y):
                        # Horizontal dimension
                        orientation = adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation
                    else:
                        # Vertical dimension
                        orientation = adsk.fusion.DimensionOrientations.VerticalDimensionOrientation

                    sketch.sketchDimensions.addDistanceDimension(
                        sketchCircle.centerSketchPoint,
                        furthest_point,
                        orientation,
                        adsk.core.Point3D.create(
                            (sketchCircle.centerSketchPoint.geometry.x + furthest_point.geometry.x) / 2,
                            (sketchCircle.centerSketchPoint.geometry.y + furthest_point.geometry.y) / 2,
                            0
                        )
                    )

                # Add radial dimension
                sketch.sketchDimensions.addRadialDimension(
                    sketchCircle,
                    adsk.core.Point3D.create(
                        centerPoint.x + radius,
                        centerPoint.y,
                        centerPoint.z
                    )
                )

            elif geometry.curveType == adsk.core.Curve3DTypes.Arc3DCurveType:
                # Handle arc edges
                arc = edge.geometry
                centerPoint = sketch.modelToSketchSpace(arc.center)
                startPoint = sketch.modelToSketchSpace(arc.startPoint)
                endPoint = sketch.modelToSketchSpace(arc.endPoint)
                radius = arc.radius
                startAngle = arc.startAngle
                endAngle = arc.endAngle

                sketchArc = sketch.sketchCurves.sketchArcs.addByCenterStartSweep(
                    centerPoint,
                    startPoint,
                    endAngle - startAngle
                )
                sketch.areDimensionsShown = True

                sketch.sketchDimensions.addRadialDimension(
                    sketchArc,
                    adsk.core.Point3D.create(
                        centerPoint.x + radius,
                        centerPoint.y,
                        centerPoint.z
                    )
                )

    except Exception as e:
        pass  # Suppress all exceptions silently

def run(context):
    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])

    except Exception as e:
        pass  # Suppress all exceptions silently

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

    # 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('Click OK to run the script.')

        try:
            run(None)
        except Exception as e:
            pass  # Suppress all exceptions silently

 

 

 

If someone can help me with this, that would be really grateful! Thank you 

0 Likes
354 Views
0 Replies
Replies (0)