Fixed SketchObjectPython

Merged, abandoned or rejected pull requests are moved here to clear the main Pull Requests forum.
Post Reply
HurdyGuigui
Posts: 38
Joined: Fri Jan 31, 2014 9:10 am

Fixed SketchObjectPython

Post by HurdyGuigui »

Hello,
I fixed, the Sketcher::SketchObjectPython :
-The functions of Sketcher::SketchObject were not accessible when creating and using a SketchObjectPython
-When creating a SketchObjectPython, the C++ execute() function is not called but the new execute function from the python code. This is a problem because the shape of the sketch was never computed. For this i created a function recomputeShape(), which is called by the c++ execute() and can be also called by python (so we can call it in the python execute() function).
I am really not sure it was the way to go, please tell me if you want it in an other way.

Here is the commit :
https://github.com/guilhemdesq/FreeCAD_ ... 8ea5f6db3b
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Fixed SketchObjectPython

Post by wmayer »

There is now a more general way to do this.

Code: Select all

class SketcherClass:
  def __init__(self, obj):
    obj.Proxy = self
  def execute(self, fp):
    fp.recompute() # when used inside the execute() method of a custom feature class it invokes the method of the C++ base class

s=App.ActiveDocument.addObject("Sketcher::SketchObjectPython")
sketcher=SketcherClass(s)
s.ViewObject.Proxy=0
paullee
Veteran
Posts: 5097
Joined: Wed May 04, 2016 3:58 pm

Re: Fixed SketchObjectPython

Post by paullee »

Hi, thanks for this feature, I am learning to use it for a while and always note there was examples with slightly different code.

I find the Sketch Python Feature object sometime fails to update recently - unknown to me is whether I have the code wrong or the difference in AppImage I download from time to time, or between Windows and Fedora Linux I work on at different time...

Before I could more systematically track down when it works or fails, some general explanation of the code / difference is appreciated:-
  1. In the execute() method, once there was 'return false', then later it was 'fp.recompute()'
  2. It seems 'fp.recompute()' works right? I have no idea what they means in fact.
  3. Running in AppImag version shown below, both 'fp.recompute()' in 'return false' are commented out in execute() method, it seem still works in Fedora Linux ?????? (But I remember this does not works in Window 7 machine with something like FreeCAD_0.17.13387_x86_dev_win.7z)
  4. Copying from somewhere else I forgot, there was 'return none' in __init__(), is it correct?
  5. Anywhere more document I can study on?
Thanks.
wmayer wrote: Sat Nov 19, 2016 3:27 pm There is now a more general way to do this.

Code: Select all

class SketcherClass:
  def __init__(self, obj):
    obj.Proxy = self
  def execute(self, fp):
    fp.recompute() # when used inside the execute() method of a custom feature class it invokes the method of the C++ base class

s=App.ActiveDocument.addObject("Sketcher::SketchObjectPython")
sketcher=SketcherClass(s)
s.ViewObject.Proxy=0
wmayer wrote: Tue Oct 20, 2015 10:47 pm With git commit e7a3dc48e834591e5553 sketcher can now be used in Python as custom feature.

So, from now on a Python feature doesn't need to have an execute() method any more. If it's not there then the execute() method of the related C++ class is called instead.

If your Python feature must have an execute() method but you also want to call the execute() of the C++ class you must return "False".

Examples:

Code: Select all

class SketchFeature:
  def __init__(self, obj):
    obj.Proxy = self
  def execute(self, obj):
    return False

SketchFeature(App.ActiveDocument.addObject("Sketcher::SketchObjectPython"))
App.ActiveDocument.ActiveObject.ViewObject.Proxy=1
OS: Linux
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13483 (Git)
Build type: None
Branch: master
Hash: 0ac32464e45fa9648cdaf702ebda06650dabe9dc
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/HongKong (en_HK)
paullee
Veteran
Posts: 5097
Joined: Wed May 04, 2016 3:58 pm

Re: Fixed SketchObjectPython

Post by paullee »

Hi, tested both in a Fedora Linux and Win 7...

Code: Select all

class SketchFeature():
  def __init__(self, obj):
      self.Type = "SketchFeature"
      obj.addProperty("App::PropertyLink","Test1","Test2","Test3")
      obj.Proxy = self
      obj.ViewObject.Proxy=0					#Not tested obj.ViewObject.Proxy=0 or 1 matters/means?
      return None
  def execute(self, fp):
      #fp.recompute commented out NOT WORKING in Win7
      #fp.recompute commented out or not both WORKS IN Linux Fedora
      fp.recompute()						# inside execute() method of custom feature class - invokes method of C++ base class
      #return False						# this is the line quoted in FC discussion earlier than above line
  def onDocumentRestored(self, fp):
      return None
Found it need 'fp.recompute()' in excute() for Win 7 to work.

Though not need 'fp.recompute() to work in Fedora Linux.

So, add 'fp.compute()' anyway.


FreeCAD_0.17.12813_x86_dev_win.7z
FreeCAD_0.17.13483.glibc2.17-x86_64.AppImage
Post Reply