Rotate object to be perpendicular to a selected CircularEdge plane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I'm trying to create a script that places screw instances to the list of selected screw holes on a body. Currently it can find the hole pivot (origin) and can put the screw into that. Currently it works if the screw is parallel to the hole. If the whole has an angle with the screw the script puts the screw in a wrong direction.
Here is the code I have currently:
command = args.firingEvent.sender
eventArgs = adsk.core.CommandEventArgs.cast(args)
# Get the values from the command inputs.
inputs = eventArgs.command.commandInputs
numberOfSelectedEdge = inputs.itemById(edgeSelectionInputId).selectionCount
# screw:
selectedBodyToInstantiate = inputs.itemById(bodySelectionInputId).selection(0).entity
# holes to put screw:
selectedEdges = []
for i in range (0, numberOfSelectedEdge ):
selectedEdges.append(inputs.itemById(edgeSelectionInputId).selection(i).entity)
#ui.messageBox(('selected: {} ').format(selectedEdges))
for edge in selectedEdges:
ent = edge
circGeom = ent.geometry
center = getCircleGeometryInfo(circGeom)
# The second point is defined using a known coordinate.
p = selectedBodyToInstantiate.transform.translation
pnt1 = adsk.core.Point3D.create(p.x, p.y, p.z)
pnt2 = adsk.core.Point3D.create(center.x, center.y, center.z)
# Create a matrix that defines the translation from point 1 to point 2.
trans: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
trans.translation = pnt1.vectorTo(pnt2)
addResult = design.activeComponent.occurrences.addExistingComponent(selectedBodyToInstantiate.component, trans)
This places the screw to the pivot (center) of circle, but as you can see in the attachment, the angle is wrong. My plan is to calculate a vector that is perpendicular to the CircularEdge plane (so a vector that goes through the hole). Then pick the same for the screw (ie. picking the CircularEdge under the screw head) calculate that vector as well, and transform the second one to the first one somehow to take the rotation. This sounds easy, but I do not know where to start, I'm not familiar with the API...
(the whole stuff is needed because the Align described here: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/api-access-to-align-components-function/td... works only for two selection, and I cannot find the programmatical way of joining/aligning two objects in Fusion 360)
Thanks for your help!