import FreeCAD as App import FreeCADGui as Gui import Part import os import re import copy from .program import LaserProgram, ViewProviderLaserProgram from .pad import LaserPad, ViewProviderLaserPad from .kuka import Kuka_Prog, Kuka_Pose, get_list_of_poses class LCCreateProgram(): def Activated(self): # Here your write what your ScriptCmd does... App.Console.PrintMessage('Create Lasser Cladding Program') a=App.ActiveDocument.addObject("App::FeaturePython","LaserProgram") LaserProgram(a) ViewProviderLaserProgram(a.ViewObject) def GetResources(self): return {'Pixmap' : ":icons/LaserCreateProg.svg", 'MenuText': 'Create Laser Program', 'ToolTip': 'Add a Laser Program to your Document'} class LCSelectBaseReference(): def Activated(self): # Here your write what your ScriptCmd does... App.Console.PrintMessage('Select Base reference!') if not Gui.Selection.hasSelection(): App.Console.PrintMessage('Select a Vertex') return # check length selection = Gui.Selection.getSelectionEx() # find first vertex for s in selection: if s.HasSubObjects: for obj in s.SubObjects: if isinstance(obj, Part.Vertex): vertex = obj.copy() laserjob_entry = App.ActiveDocument.getObject('LaserProgram') if laserjob_entry is None: App.Console.PrintMessage('Create a LaserJob first') return laserjob_entry.base_reference = App.Vector((vertex.X, vertex.Y, vertex.Z)) App.ActiveDocument.recompute() def GetResources(self): return {'Pixmap' : ":icons/LaserSelectBaseRef.svg", 'MenuText': 'Select Base reference', 'ToolTip': 'Add a Job to your Document'} class LCCreatePad(): def _create_laserpad(self, laserprogram, ref_to_face): #laserprogram = App.ActiveDocument.getObject('LaserProgram') #if laserprogram is None: # App.Console.PrintMessage('Create a LaserProgram first') # return pad_obj = laserprogram.newObject("App::FeaturePython","LaserPad") LaserPad(pad_obj, ref_to_face) ViewProviderLaserPad(pad_obj.ViewObject) return pad_obj def Activated(self): # Here your write what your ScriptCmd does... App.Console.PrintMessage('Select some Face as reference') if not Gui.Selection.hasSelection(): App.Console.PrintMessage('Select a Face') return # check length selection = Gui.Selection.getSelectionEx() if len(selection) != 2: App.Console.PrintMessage('Select a Face and a LaserProgram') return # find the Program prog = None face = None if selection[0].Object.Name.startswith('LaserProgram'): prog = selection[0].Object ref_face = (selection[1].Object, Gui.Selection.getSelectionEx()[1].SubElementNames[0]) elif selection[1].Object.Name.startswith('LaserProgram'): prog = selection[1].Object ref_face = (selection[0].Object, Gui.Selection.getSelectionEx()[0].SubElementNames[0]) else: App.Console.PrintMessage('Please select a LaserProgram') return #if program is None: # App.Console.PrintMessage('Please select a LaserProgram') # return #ref_face = (Gui.Selection.getSelection()[0], # Gui.Selection.getSelectionEx()[0].SubElementNames[0]) #selection = Gui.Selection.getSelectionEx() # find first vertex #for s in selection: # if s.HasSubObjects: # for obj in s.SubObjects: # if isinstance(obj, Part.Face): # face = obj.copy() self._create_laserpad(prog, ref_face) App.ActiveDocument.recompute() def GetResources(self): return {'Pixmap' : ":icons/LaserCreatePad.svg", 'MenuText': 'Create Pad', 'ToolTip': 'Create a Pad on selected face'} class LCRecompute(): def Activated(self): # Here your write what your ScriptCmd does... App.Console.PrintMessage('Recomputer Pads') if not Gui.Selection.hasSelection(): App.Console.PrintMessage('Select a Pad') return if Gui.Selection.hasSelection(): laser_path_obj = Gui.Selection.getSelection()[0] if len(laser_path_obj.Group): laser_path_obj.removeObjectsFromDocument() laser_path_obj.Proxy._create_path(laser_path_obj) App.ActiveDocument.recompute() def GetResources(self): return {'Pixmap' : ":icons/LaserRecomputePad.svg", 'MenuText': 'Recompute', 'ToolTip': 'Recompute selected Pads'} class LCSaveProg(): def Activated(self): # Here your write what your ScriptCmd does... App.Console.PrintMessage('Saving to KRL') if not Gui.Selection.hasSelection(): App.Console.PrintMessage('Select a Face') return selection = Gui.Selection.getSelectionEx() c = None if selection[0].Object.Name.startswith('LaserProgram'): c = selection[0].Object if c is None: App.Console.PrintMessage('Selection is not a LaserProgram') #c = App.ActiveDocument.getObject("LaserProgram") pads = c.Group prog = Kuka_Prog() prog.set_baseorigin(c.base_reference) prog.set_tool(c.rob_tool) prog.set_base(c.rob_base) prog.set_velocity(c.laser_feedrate, c.laser_speed) prog.set_laser_power(c.laser_power) prog.set_laser_out(c.laser_real_out) prog.set_powder_out(c.laser_powder_out) prog.set_inert_gas_out(c.laser_gas_out) prog.set_simulation(c.simulation) prog.set_label(c.Label) for pad in pads: # one pad with contours and hatchlines prog.create_layer() for progpart in pad.Group: ## jedes Contour oder Hatchline Feature if re.match('Contour*', progpart.Name): # do Conoutes face = pad.ref_body.getSubObject(pad.ref_surface) #edges = Part.__sortEdges__(progpart.Shape.Edges) wires = progpart.Shape.Wires print("Number Contourlines (should be 1): ", len(wires)) counter = 0 for wire in wires: line = [] for edge in Part.__sortEdges__(wire.Edges): p0 = edge.Vertexes[0].Point p1 = edge.Vertexes[1].Point for p in [p0, p1]: uv = face.Surface.parameter(p) normal = face.normalAt(uv[0], uv[1]) pose = Kuka_Pose.from_point_and_normal(p, normal) line.append(pose) print("append contour line (segment): ", counter) counter += 1 pathtype = getattr(progpart, 'pathtype', 'LIN') prog.append_contour(line, pathtype) #vlist = [] #for edge in edges: # vlist.extend([v.Point for v in edge.Vertexes]) #poly = Part.makePolygon(vlist) ##Part.show(poly, "ContourPath") #poses = get_list_of_poses(face, poly.Edges) elif re.match('Hatch*', progpart.Name): face = pad.ref_body.getSubObject(pad.ref_surface) wires = progpart.Shape.Wires print("Number Hatchlines: ", len(wires)) counter = 0 for wire in wires: line = [] for edge in wire.Edges: p0 = edge.Vertexes[0].Point p1 = edge.Vertexes[1].Point for p in [p0, p1]: uv = face.Surface.parameter(p) normal = face.normalAt(uv[0], uv[1]) pose = Kuka_Pose.from_point_and_normal(p, normal) line.append(pose) print("append line: ", counter) counter += 1 prog.append_hatchline(line, progpart.pathtype) #prog.save_prog(c.article, c.progpath) if c.templatename is None: templatename = "lasercladding" else: templatename = c.templatename prog.save_with_template(c.article, c.progpath, templatename) App.ActiveDocument.recompute() def GetResources(self): return {'Pixmap' : ":icons/LaserSaveProg.svg", 'MenuText': 'Save Program', 'ToolTip': 'Save the Program as KRL'} Gui.addCommand('LCCreateProgram', LCCreateProgram()) Gui.addCommand('LCSelectBaseReference', LCSelectBaseReference()) Gui.addCommand('LCCreatePad', LCCreatePad()) Gui.addCommand('LCRecompute', LCRecompute()) Gui.addCommand('LCSaveProg', LCSaveProg())