How to transform sketches correctly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, I am struggling with a problem. I have a situation like this: A component with a mesh body and several sketches. I am trying to align the mesh and the sketches so that they are aligned with the occurrences origin. I have a sketch name "Axis" with a single sketch line that I would like to align with my occurrences Z axis, and a sketch named Center, that I would like to align with the origin of the occurrence. Then after I move the mesh and the sketches I want to transform the occurrence back to where it was originally. I am able to get the mesh to align correctly, but I am struggling with getting the sketches to position correctly.
Here is what I have so far.
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
design:adsk.fusion.Design = app.activeDocument.products.itemByProductType("DesignProductType")
root = design.rootComponent
occurrence = root.occurrences.item(0)
component = occurrence.component
originalPosition = occurrence.transform2
for sketch in component.sketches:
if "Center" in sketch.name:
center = sketch
if "Axis" in sketch.name:
axis = sketch
#align the axis vector with the occurrence Z axis
zVector = axis.sketchCurves.sketchLines.item(0).worldGeometry.asInfiniteLine().direction
transformationMatrix = adsk.core.Matrix3D.create()
transformationMatrix.setToRotateTo(zVector,adsk.core.Vector3D.create(0,0,1))
#get the new location of the center after the rotation has been applied
center = center.sketchPoints.item(1).worldGeometry
center.transformBy(transformationMatrix)
#apply the translation so the center sketch point is on the occurence origin
translationVector = center.vectorTo(adsk.core.Point3D.create(0,0,0))
transformationMatrix.translation = translationVector
#align the mesh
moveFeatures = component.features.moveFeatures
objectsToMove = adsk.core.ObjectCollection.create()
for body in occurrence.bRepBodies:
objectsToMove.add(body)
objectsToMove.add(component.meshBodies.item(0))
moveMesh = moveFeatures.createInput2(objectsToMove)
moveMesh.defineAsFreeMove(transformationMatrix)
moveFeatures.add(moveMesh)
#align the sketches
for sketch in component.sketches:
mat = sketch.transform
#get the sketch in world space
mat.transformBy(occurrence.transform2)
#transform the sketch using the same matrix we used to transform the mesh
mat.transformBy(transformationMatrix)
sketch.transform = mat
#move the occurrence back to where all the items originally where
transformationMatrix.invert()
transformationMatrix.transformBy(originalPosition)
occurrence.transform2 = transformationMatrix
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
I have also attached a test project I have been using.
Any help is greatly appreciated.