Skip to content

Instantly share code, notes, and snippets.

@kwahoo2
Created December 7, 2024 23:00
Show Gist options
  • Save kwahoo2/7e9f449e7d9d67d3eea6280d183eaefe to your computer and use it in GitHub Desktop.
Save kwahoo2/7e9f449e7d9d67d3eea6280d183eaefe to your computer and use it in GitHub Desktop.
import FreeCADGui as Gui
import FreeCAD as App
import Part
def pick_ray(start_vec, dir_vec):
Gui.Selection.clearSelection()
if App.ActiveDocument.getObject('Line_dir'):
App.ActiveDocument.removeObject('Line_dir')
if App.ActiveDocument.getObject('Line_ray'):
App.ActiveDocument.removeObject('Line_ray')
if App.ActiveDocument.getObject('Sphere_sect'):
App.ActiveDocument.removeObject('Sphere_sect')
view = Gui.ActiveDocument.ActiveView
info = view.getObjectInfoRay(start_vec, dir_vec)
App.ActiveDocument.addObject("Part::Line","Line_dir") # line have to be created after picking to avoid intersection
App.ActiveDocument.Line_dir.Label='Dir'
App.ActiveDocument.Line_dir.X1=start_vec.x
App.ActiveDocument.Line_dir.Y1=start_vec.y
App.ActiveDocument.Line_dir.Z1=start_vec.z
App.ActiveDocument.Line_dir.X2=start_vec.x + dir_vec.x
App.ActiveDocument.Line_dir.Y2=start_vec.y + dir_vec.y
App.ActiveDocument.Line_dir.Z2=start_vec.z + dir_vec.z
if (info):
sect_pt = info['PickedPoint']
App.ActiveDocument.addObject("Part::Line","Line_ray") # line have to be created after picking to avoid self-intersection
App.ActiveDocument.Line_ray.Label='Ray'
Gui.ActiveDocument.Line_ray.LineColor = (1.00,0.67,0.00)
App.ActiveDocument.Line_ray.X1=start_vec.x
App.ActiveDocument.Line_ray.Y1=start_vec.y
App.ActiveDocument.Line_ray.Z1=start_vec.z
App.ActiveDocument.Line_ray.X2=sect_pt.x
App.ActiveDocument.Line_ray.Y2=sect_pt.y
App.ActiveDocument.Line_ray.Z2=sect_pt.z
App.ActiveDocument.addObject("Part::Sphere","Sphere_sect")
App.ActiveDocument.Sphere_sect.Label = "Intersection point"
App.ActiveDocument.Sphere_sect.Radius = '0.5 mm'
App.ActiveDocument.Sphere_sect.Placement = App.Placement(sect_pt,App.Rotation(App.Vector(0.00,0.00,1.00),0.00))
Gui.ActiveDocument.Sphere_sect.ShapeColor = (1.00,0.33,0.00)
Gui.Selection.addSelection(info['Document'], info['Object'], info['Component'], sect_pt.x, sect_pt.y, sect_pt.z)
print(info)
App.ActiveDocument.recompute()
pick_ray(App.Vector(-3,-10, 1), App.Vector(0.2, 0.5, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment