Centroid don't set 3D point on the center

Centroid don't set 3D point on the center

Anonymous
Not applicable
576 Views
2 Replies
Message 1 of 3

Centroid don't set 3D point on the center

Anonymous
Not applicable

Hello,

This is my first post here and i'm pretty new to the API environnement.

Anyway, im trying to do a simple add-in to get better and do something fancy after that. My code is actually pretty simple :

- get 3 user inputs : a face, and 2 values (diameter and lenght)

-add a sketch on the selected face

-add a point at centroid

-do a hole at the point (draw the circle, with the inputs values diam and length)

-draw a circle at the point (with the input diam)

-extrude the circle (w/ the input length)

-do a 1 mm chamfer at the end faces of the extrusions

 

So right now the add-in is working sometimes, when the point for some reason is properly set all the code is running fine. But, for some reasons the point is often set at random positions and not on the sketch!! here's a screenshot :

Capture.JPG

i'm stuck on this issue since few days... if someone have a solution it will be nice!

 

Thank you 🙂

 

 

here's my code:

 

#Author-Ewan Le Moal
#Description- Create a single hole and a dowel in the middle of the selected plane

import adsk.core, adsk.fusion, adsk.cam, traceback


handlers = []

activeDoc = None

unit = 'mm'

class SingleHoleCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()

def notify(self, args):
try:
app = adsk.core.Application.get()
ui = app.userInterface

global activeDoc
activeDoc = app.activeDocument

# Define the command dialog.
cmd = adsk.core.Command.cast(args.command)
cmdInputs = cmd.commandInputs

planeInput = cmdInputs.addSelectionInput('planeSelect', 'Select Plane', 'Select Plane')
planeInput.addSelectionFilter('PlanarFaces')
planeInput.setSelectionLimits(1)

cmdInputs.addValueInput('diamCheville', 'Diamètre des chevilles', unit, adsk.core.ValueInput.createByReal(0.0))

cmdInputs.addValueInput('profCheville', 'Profondeur des chevilles', unit, adsk.core.ValueInput.createByReal(0.0))

onExecute = SingleHoleCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)

except:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class SingleHoleCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()

def notify(self, args):
try:
inputs = args.command.commandInputs
result = getInput(inputs)

app = adsk.core.Application.get()
HoleOrigin(result[0], result [1], result [2])
except:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def getInput(inputs):
try:
for input in inputs:
if input.id == 'planeSelect':
PlaneEnt = input.selection(0).entity
elif input.id == 'diamCheville':
diamCheville = input.value
elif input.id == 'profCheville':
profCheville = input.value

return (PlaneEnt, diamCheville, profCheville)
except:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def HoleOrigin(PlaneEnt, diamCheville, profCheville):
try:
app = adsk.core.Application.get()
des = adsk.fusion.Design.cast(app.activeProduct)

sk = des.rootComponent.sketches.add(PlaneEnt)


point = PlaneEnt.centroid
sketchPoints = sk.sketchPoints.add(point)

ptColl = adsk.core.ObjectCollection.create()
ptColl.add(sketchPoints)

holes = des.rootComponent.features.holeFeatures
holeInput = holes.createSimpleInput(adsk.core.ValueInput.createByReal(diamCheville))
holeInput.setPositionBySketchPoints(ptColl)
holeInput.setDistanceExtent(adsk.core.ValueInput.createByReal(profCheville))
hole = holes.add(holeInput)

circle = sk.sketchCurves.sketchCircles.addByCenterRadius(sketchPoints, diamCheville/2)
profColl = adsk.core.ObjectCollection.create()
for prof in sk.profiles:
if prof.profileLoops.count == 1:
profColl.add(prof)


input = des.rootComponent.features.extrudeFeatures.createInput(profColl,adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
input.setDistanceExtent(True, adsk.core.ValueInput.createByReal(profCheville))
extrude = des.rootComponent.features.extrudeFeatures.add(input)

Body = extrude.endFaces.item(0)
Edges = Body.edges.item(0)
EdgesColl = adsk.core.ObjectCollection.create()
EdgesColl.add(Edges)


chamfersinput = des.rootComponent.features.chamferFeatures.createInput(EdgesColl, False)
chamfersinput.setToEqualDistance(adsk.core.ValueInput.createByReal(0.1))
chamfer = des.rootComponent.features.chamferFeatures.add(chamfersinput)





except:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface

existingDef = ui.commandDefinitions.itemById('SingleHole')
if existingDef:
existingDef.deleteMe()

SingleHoleCmdDef = ui.commandDefinitions.addButtonDefinition('SingleHole', 'Single Hole', 'Create a single hole in the middle of the selected plane','./Resources/SingleHole')

onCommandCreated = SingleHoleCommandCreatedHandler()
SingleHoleCmdDef.commandCreated.add(onCommandCreated)
handlers.append(onCommandCreated)

createPanel = ui.allToolbarPanels.itemById('SolidCreatePanel')

createPanel.controls.addCommand(SingleHoleCmdDef, 'FusionHoleCommand', False)

except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface

createPanel = ui.allToolbarPanels.itemById('SolidCreatePanel')
SingleHoleControl = createPanel.controls.itemById('SingleHole')
if SingleHoleControl:
SingleHoleControl.deleteMe()

existingDef = ui.commandDefinitions.itemById('SingleHole')
if existingDef:
existingDef.deleteMe()



except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

0 Likes
577 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @Anonymous .

 

PlaneEnt.centroid returns world coordinate values.
If the sketch origin is different from the world coordinate origin, it is necessary to convert the misaligned values and incorporate them into the sketch.

 

・・・
def HoleOrigin(PlaneEnt, diamCheville, profCheville):
    try:
        app = adsk.core.Application.get()
        des = adsk.fusion.Design.cast(app.activeProduct)

        sk = des.rootComponent.sketches.add(PlaneEnt)

        point = PlaneEnt.centroid

        # clone point
        pnt :adsk.core.Point3D = point.copy()

        # get sketch matrix3d
        mat3d :adsk.core.Matrix3D = sk.transform
        mat3d.invert()

        # point transform
        pnt.transformBy(mat3d)

        # point add sketch
        sketchPoints = sk.sketchPoints.add(pnt)

        ptColl = adsk.core.ObjectCollection.create()
        ptColl.add(sketchPoints)
・・・
0 Likes
Message 3 of 3

BrianEkins
Mentor
Mentor

You can also use the Sketch.modelToSketchSpace function.  It avoids having to understand how the matrix is used.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com