- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.')
Solved! Go to Solution.