Hi @SaeedHamza .
When I tried this in the past, I was able to get the coordinates I wanted by using MouseEventArgs.viewportPosition.
However, since the object obtained was a Point2D, it needed to be calculated.
The following sample shows how to run the script while working on a sketch, and it will display the coordinate values on the sketch every time you click on it.
# Fusion360API Python script
import traceback
import adsk.cam
import adsk.fusion
import adsk.core
_app: adsk.core.Application = None
_ui: adsk.core.UserInterface = None
_handlers = []
class MyMouseClickHandler(adsk.core.MouseEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.MouseEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
# Current mouse cursor position.
cursor2D: adsk.core.Point2D = args.viewportPosition
# get viewport.
vp: adsk.core.Viewport = args.viewport
# Convert from view coordinates to 3D coordinates.
pos3d: adsk.core.Point3D = vp.viewToModelSpace(cursor2D)
# Get a sketch of the work in progress.
global _app
skt: adsk.fusion.Sketch = _app.activeEditObject
# Get the geometry of the sketch plane.
# But I think this method does not work in direct mode.
refPlane = skt.referencePlane
plane: adsk.core.Plane = refPlane.geometry
# Get vector of eye-target from camera.
# In short, get the direction you are facing.
cam: adsk.core.Camera = vp.camera
vec: adsk.core.Vector3D = cam.eye.vectorTo(cam.target)
# Create an infinite line of the camera direction passing through the mouse cursor position.
infiniteLine: adsk.core.InfiniteLine3D = adsk.core.InfiniteLine3D.create(
pos3d, vec
)
# Obtain the intersection of a plane and an infinite line.
interPnt: adsk.core.Point3D = plane.intersectWithLine(infiniteLine)
# Convert to coordinates on the sketch.
posSkt: adsk.core.Point3D = skt.modelToSketchSpace(interPnt)
# Calculate the ratio for unit conversion.
des: adsk.fusion.Design = _app.activeProduct
unitMgr: adsk.core.UnitsManager = des.unitsManager
unitInter = unitMgr.internalUnits
unitDef = unitMgr.defaultLengthUnits
ratio: float = unitMgr.convert(1, unitInter, unitDef)
# Update the coordinate values of the dialog.
global _txtIptInfo
inputs: adsk.core.CommandInputs = args.firingEvent.sender.commandInputs
txtIpt: adsk.core.TextBoxCommandInput = inputs.itemById('txtIpt')
txtIpt.text = f'X:{posSkt.x * ratio}\nY:{posSkt.y * ratio}\nZ:{posSkt.z * ratio}'
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandCreatedEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
try:
global _handlers
cmd: adsk.core.Command = adsk.core.Command.cast(args.command)
cmd.isOKButtonVisible = False
# event
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
onMouseClick = MyMouseClickHandler()
cmd.mouseClick.add(onMouseClick)
_handlers.append(onMouseClick)
# inputs
inputs: adsk.core.CommandInputs = cmd.commandInputs
inputs.addTextBoxCommandInput(
'txtIpt',
'position',
'-',
3,
True
)
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
adsk.terminate()
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# Quit the script when you are not working on a sketch.
actEditObj = _app.activeProduct.activeEditObject
if actEditObj.classType() != adsk.fusion.Sketch.classType():
_ui.messageBox(
'This can only be done in a sketch task.'
)
return
cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
'test_cmd'
)
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition(
'test_cmd',
'Test',
'Test'
)
global _handlers
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
cmdDef.execute()
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
I don't think it will work correctly in direct mode or sketching in deep Occurrence.
There may be an easier way to get the coordinate values, but this is the only way I could find.