[Solved] Custom Workbench - def execute(self,fp): runs when I delete an element in Sketch

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
prandall
Posts: 76
Joined: Thu Feb 16, 2017 12:42 pm
Contact:

[Solved] Custom Workbench - def execute(self,fp): runs when I delete an element in Sketch

Post by prandall »

OS: Linux Mint 17.3 Rosa
Word size of OS: 32-bit
Word size of FreeCAD: 32-bit
Version: 0.18.15353 (Git)
Build type: Release
Branch: master
Hash: 20fe774784bd013c74c1edaed8985e851bb1370b
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)

Hello all,

I am attempting to add some sketches, that I create over and over, into a workbench. I am also trying to learn to code Python and implement Workbenches. I have the following command as an example. Mostly I have just followed examples and hacked my way through so far. I have included a test command demonstrating my approach. The shapes in the long run will be more complicated.

My problem is that when I click the command button it creates a sketch with two lines as expected. However, if I delete a line the execute() method gets called and adds two additional lines. So I am not sure where I should put the sketch code or more importantly why the execute() is getting called. I have used this approach for Part::FeaturePython objects and it seems to work fine, but clearly I am not fully understanding the execute() method.

I did look around in forums for awhile but I don't even really know what I am looking for.

Thank you so much in advance.

Code: Select all

import FreeCAD,FreeCADGui,Part, Sketcher
import os

class MyLine_Command:

	def GetResources(self):

		return {"MenuText": "MyLine",
			"ToolTip": "Tooltip for MyLine command" } 

	def IsActive(self):
		if FreeCAD.ActiveDocument == None:
			return False
		else:
			return True

	def Activated(self):
		c=FreeCAD.ActiveDocument.addObject('Sketcher::SketchObjectPython','MyLine')
		print "Sketcher Object TyepID: " + c.TypeId		
		sketcherObject = MyLine(c)
		c.ViewObject.Proxy=0

		#FreeCAD.ActiveDocument.recompute()

class MyLine:

	def __init__(self, obj):
	        obj.Proxy = self

	def onChanged(self, fp, prop):
		name = str(prop)
		newvalue = str(fp.getPropertyByName(str(prop)))

	def execute(self,fp):
		geometryList = []
		geometryList.append( Part.LineSegment(FreeCAD.Vector(-44.546316,-2.496507,0),FreeCAD.Vector(44.830833,-2.496507,0)))
		geometryList.append( Part.LineSegment(FreeCAD.Vector(-44.546316,2.503493,0),FreeCAD.Vector(44.544970,2.503493,0)))
		fp.addGeometry( geometryList, False)		
		fp.recompute()

FreeCADGui.addCommand('MyLine', MyLine_Command())
Last edited by prandall on Sat Dec 22, 2018 10:30 pm, edited 1 time in total.
3D Printing, CAD, Electronics and other errata at: https://www.youtube.com/channel/mathcodeprint
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Custom Workbench - def execute(self,fp): runs when I delete an element in Sketch

Post by microelly2 »

you can create the two lines in the _init_ method,
so they are created only in the first call.
User avatar
prandall
Posts: 76
Joined: Thu Feb 16, 2017 12:42 pm
Contact:

Re: Custom Workbench - def execute(self,fp): runs when I delete an element in Sketch

Post by prandall »

microelly2 wrote: Wed Dec 19, 2018 11:27 am you can create the two lines in the _init_ method,
so they are created only in the first call.
Thanks, so simple, I had in my mind that __init__ is reserved somehow.
3D Printing, CAD, Electronics and other errata at: https://www.youtube.com/channel/mathcodeprint
Post Reply