How to extend an part class inside python?

Need help, or want to share a macro? Post here!
User avatar
microelly2
Posts: 4446
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: How to extend an part class inside python?

Postby microelly2 » Thu Jun 15, 2017 4:28 pm

A first example to use Sketcher::SketchObjectPython
A Sketch which computes some objects on base of its inner geometry.

https://youtu.be/U31O5vW4UhI
sketch_python.py
(1.91 KiB) Downloaded 80 times
User avatar
bernd
Posts: 8475
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: How to extend an part class inside python?

Postby bernd » Fri Jun 16, 2017 10:22 pm

wow no rain on weekends for nearly two years :shock: :lol: :mrgreen:
paullee
Posts: 1827
Joined: Wed May 04, 2016 3:58 pm

Re: How to extend an part class inside python?

Postby paullee » Fri Jan 12, 2018 1:01 pm

Hi, I am trying to figure out how this works and the possibilities.

(Microelly2 has a number of examples which are currently too advanced for me to understand :oops: )


Just have a question in the meantime:

If there is already a sketch done and I want to extend this sketches with features in a Sketcher::SketchObjectPython e.g. the feedback sketch Microelly2 ...

- is that a way to 'upgrade' this sketch?

- or need to 'copy' all the content of this sketch (geometries - normal and construction, constraints - driving and non-driving, expressions etc.) to the new Sketcher::SketchObjectPython object?

I hope my question make sense at all :)

Thanks !
User avatar
microelly2
Posts: 4446
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: How to extend an part class inside python?

Postby microelly2 » Fri Jan 12, 2018 2:16 pm

paullee wrote:
Fri Jan 12, 2018 1:01 pm
Hi, I am trying to figure out how this works and the possibilities.

(Microelly2 has a number of examples which are currently too advanced for me to understand :oops: )


Just have a question in the meantime:

If there is already a sketch done and I want to extend this sketches with features in a Sketcher::SketchObjectPython e.g. the feedback sketch Microelly2 ...

- is that a way to 'upgrade' this sketch?

- or need to 'copy' all the content of this sketch (geometries - normal and construction, constraints - driving and non-driving, expressions etc.) to the new Sketcher::SketchObjectPython object?

I hope my question make sense at all :)

Thanks !
It is possible to copy the content of a sketch to a new/empty SketchPythonObject and then use the methods of this python object.
I do this when I get common sketches and want to add functionality. So old sketches can be reused.

I don't know whether it is possible to upgrade a sketch.
paullee
Posts: 1827
Joined: Wed May 04, 2016 3:58 pm

Re: How to extend an part class inside python? [EDIT]

Postby paullee » Sat Jan 13, 2018 1:53 am

microelly2 wrote:
Fri Jan 12, 2018 2:16 pm

It is possible to copy the content of a sketch to a new/empty SketchPythonObject and then use the methods of this python object.
I do this when I get common sketches and want to add functionality. So old sketches can be reused.

I don't know whether it is possible to upgrade a sketch.
Many thanks!

A few more questions when I explore a little bit more...
  1. Following Wmayer's example code https://forum.freecadweb.org/viewtopic. ... 29#p103443 but when it come to App.ActiveDocument.ActiveObject.ViewObj.... the 'ViewObject' is not auto-completed. Seem it is not there but input the whole code return no error. No idea what does it imply.
  2. I haven't found a method (other than sketch.addCopy... ) to copy all 'contents' from a sketch to another, any exist?
  3. One of the problem is copying expressions, e.g.:
    Sketch A <--- Sketch B has expressions linked to Sketch A
    Now,
    New 'Sketch A content SketchPythonObject' <--- New 'Sketch B content SketchPythonObject'
    Then, all the expressions in original Sketch B needs to 're-interpret' to new 'Sketch A SkethPythonObject' !
  4. Another problem seems within objects other than Arch. Arch Object (all or mostly?) has a Source property - if Sketch A become Sketch A content SketchPythonObject, the parent 'Source' could now linked to the new object I guess. With Part object like extrusion, it seem it can't.

    ...
Emm... now I have an idea / concept ... before I manage to even try, do you see it make sense or is possible at all?
  1. Original Sketch A (not SketchPythonObject) (full of geometries, expressions, constraints...)
  2. New 'Sketch X SketchPythonObject' ---> Python 'manipulate the content' of Sketch A (instead of 'geometries' in Sketch X SketchPythonObject)
  3. So no 'transfer' needed, dependencies on original Sketch A needs not be tracked down and changed...
  4. (or does it need to be 'SketchPythonObject' at all if after all possible?)
  5. (and will it be a problem if Sketch A is updated, how 'python in SketchX know... and execute code?...break other parents recompute process?'...
BTW, I remember somewhere in the forum that there is way to 'grey out' / prevent from edit an originally available input box in an object, e.g. Placement of Sketch001. Can't find it... [EDIT] It seems there are some hints to follow here & here... https://www.freecadweb.org/wiki/Scripted_objects ... https://forum.freecadweb.org/viewtopic.php?t=12928

Too many questions...!