For those who still have problem with this, especially in cases when sketches lays on planes created by angles, three points, cones' surfaces, etc. theres slightly simple solution. In all those cases you don't need to construct or get normal from planes/sketches. The only thing you need to do is to proper create of matrix based on origin and angle, no mather what direction of plane's normal is. I realized it after some samples. Here are my script for testing rotation of sketches on three different planes. It prepares plane, sketch on it (line) and later, rotate those sketches. The main function is sampleSketchRotation() and rotates are done in rotateSketch(sketch, angle, origin) function.
def handleErrorMessage(traceinfo, displayRawException: bool = False):
if ui:
if displayRawException:
ui.messageBox('Error(s) occured:\n\n{}'.format(traceinfo.format_exc()))
else:
formattedLines = traceinfo.format_exc().splitlines()
ui.messageBox('Error(s) occured\n\nReason:\n{}\n\nLine:\n{}\n\nPlace:\n{}'.format(formattedLines[3],formattedLines[2],formattedLines[1]))
def prepareSketch(sketches, plane):
try:
sketch = sketches.add(plane)
lines = sketch.sketchCurves.sketchLines
p_1 = adsk.core.Point3D.create(0, 0, 0)
p_2 = adsk.core.Point3D.create(20, 0, 0)
lines.addByTwoPoints(p_1, p_2)
return sketch
except:
handleErrorMessage(traceback)
def displayMatrixCells(matrix):
lines = ''
for r in range(0, 4):
line = ''
for c in range(0, 4):
value = matrix.getCell(r, c)
if abs(value) < 0.000000001:
value = 0
line += format(value, ">-8.2f")
lines += line + '\n'
ui.messageBox(lines)
def getRotationMatrix(angle, origin):
try:
matrix = adsk.core.Matrix3D.create()
normal = adsk.core.Vector3D.create(0, 0, 1)
matrix.setToRotation(angle, normal, origin)
return matrix
except:
handleErrorMessage(traceback)
def rotateSketch(sketch, angle, origin):
try:
all = adsk.core.ObjectCollection.create()
for c in sketch.sketchCurves:
all.add(c)
for p in sketch.sketchPoints:
all.add(p)
matrix = getRotationMatrix(angle, origin)
#displayMatrixCells(matrix)
sketch.move(all, matrix)
return sketch
except:
handleErrorMessage(traceback)
def sampleSketchRotation():
try:
# get document and prepare other properties
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct
rootComp = design.rootComponent
sketches = rootComp.sketches
xyPlane = rootComp.xYConstructionPlane
planes = rootComp.constructionPlanes
rotationAngle = math.pi / 2
#origin = adsk.core.Point3D.create(0, 0, 0)
origin = adsk.core.Point3D.create(1, 1, 0)
# xyPlane
firstSketch = prepareSketch(sketches, xyPlane)
firstSketch.name = 'xyPlane sketch'
firstSketch = rotateSketch(firstSketch, rotationAngle, origin)
# plane rotated around x-axis
p_1 = adsk.core.Point3D.create(-2, 0, 0)
p_2 = adsk.core.Point3D.create(2, 0, 0)
rotationAxis = firstSketch.sketchCurves.sketchLines.addByTwoPoints(p_1, p_2)
planeInput = planes.createInput()
planeAngleInput = adsk.core.ValueInput.createByString('30.0 deg')
planeInput.setByAngle(rotationAxis, planeAngleInput, xyPlane)
rotatedPlane = planes.add(planeInput)
rotatedPlane.name = 'Rotated plane'
secondSketch = prepareSketch(sketches, rotatedPlane)
secondSketch.name = 'Rotated plane sketch'
secondSketch = rotateSketch(secondSketch, rotationAngle, origin)
# plane created based on three points
pos_3 = adsk.core.Point3D.create(0, 0, 1)
pos_4 = adsk.core.Point3D.create(2, 0, 0)
pos_5 = adsk.core.Point3D.create(0, -3, 0)
p_3 = firstSketch.sketchPoints.add(pos_3)
p_4 = firstSketch.sketchPoints.add(pos_4)
p_5 = firstSketch.sketchPoints.add(pos_5)
planeInput.setByThreePoints(p_3, p_4, p_5)
threePointsPlane = planes.add(planeInput)
threePointsPlane.name = 'Three-points plane'
thirdSketch = prepareSketch(sketches, threePointsPlane)
thirdSketch.name = 'Three-points plane sketch'
thirdSketch = rotateSketch(thirdSketch, rotationAngle, origin)
except:
handleErrorMessage(traceback)