Image WB in Python

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Image WB in Python

Post by JAndersM »

I made the beginnings of a Image workbench in Python.
Link: https://github.com/JAndersM/Image2
You can import image(s), create image plane(s), move an scale the plane. Also make it semi-transparent.
image2-1.png
image2-1.png (311.62 KiB) Viewed 3156 times
I would appreciate help by someone that actually knows what he/she is doing when it comes to developing FreeCAD. Am I on the right track?

Some specific questions/thoughts:
* Feature type: After experimenting and checking different examples I choose "Part::Part2DObjectPython" for the image plane. That way I got position etc to work. But there seems to be something strange with the texture application, texture coordinates seems not to have any effect. Is there a better choice of part type or something I have missed about textures?
* Image view: I thought of having an image view similar to the original Image WB showing the image in 2D. How do you create such a view?
* Translation: how to prepare for translation?

I have an idea of making it possible to create image planes from selected rectangular regions of the same image. (Many blueprints have different views on the same document so I think this would be a useful feature.) To get this to work I first need to get texture coordinates and image view to work,
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Image WB in Python

Post by ickby »

Hello,

Nice results already. I try to give some advice:

Feature type: Part2DObject is not a good choice, as this object has quite some overhead and lots of functionality you do not use. For one it makes image dependent on the Part workbench, which is not very nice as it really needs absolutly no functionality from it. On the other hand you have the Shape property which is not needed at all. In your case I think it is best to create your object directly from DocumentObject. Than you just add a Placement property to your object directly. In your viewprovider you watch for changes of the pacement property, and if one happens you simply update a Transform node you have added in the scene graph. I did not yet work with textures in open inventor, but should not be too hard.

Image view: For this you use PySide and create a nice QT GUI. To learn how to create a image viewer itself have a look at https://github.com/PySide/Examples/blob ... eviewer.py . Adding it to FreeCAD is easy: Search the MainWindow for the MDI area and add a new view to it (all via PySide API). This new view than holds the image viewer.

Translation; That I don't know.
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image WB in Python

Post by JAndersM »

Thanks!
ickby wrote:Hello,
In your case I think it is best to create your object directly from DocumentObject. Than you just add a Placement property to your object directly. In your viewprovider you watch for changes of the pacement property, and if one happens you simply update a Transform node you have added in the scene graph.
Any example? I did try to add placement property to a PythonFeature (DocumentObject) and watch for changes but could not get it to work. So I must have missed something. (On the other hand using PythonFeature the texture coordinates did work.)
ickby wrote: Image view: For this you use PySide and create a nice QT GUI. To learn how to create a image viewer itself have a look at https://github.com/PySide/Examples/blob ... eviewer.py . Adding it to FreeCAD is easy: Search the MainWindow for the MDI area and add a new view to it (all via PySide API). This new view than holds the image viewer.
OK will give it a try.
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Image WB in Python

Post by ickby »

Any example? I did try to add placement property to a PythonFeature (DocumentObject) and watch for changes but could not get it to work. So I must have missed something. (On the other hand using PythonFeature the texture coordinates did work.)
Unfortunatly I can't give you an example, 'm coding in c++ only. But I'm pretty sure the Python methods are eqaul to c++ ones: In an DocumentObject you have the onChanged method which gets called if a property changed. There you can query the properties name and do somethign when the placement property is changed. Of course you want to do something in the viewprovider, as there you have access to the scene graph. In a viewprovider you have two such callback functions, one for the Properties of the Viewprovider, called onChanged, and one for the properties of the DocumentObject, called updateData. In this one you should find the change of the placement property.
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: Image WB in Python

Post by ian.rees »

This looks like something I would've used just last weekend! I made a little 3D-printed fixture to hold a small part based mostly on a couple jpegs. Ended up using inkscape to extract coordinates to transpose them into Sketcher - here's the outcome:
Screen Shot 2016-02-15 at 7.46.57 pm.png
Screen Shot 2016-02-15 at 7.46.57 pm.png (66.65 KiB) Viewed 3100 times
Keep up the good work! -Ian-
User avatar
wandererfan
Veteran
Posts: 6320
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Image WB in Python

Post by wandererfan »

JAndersM wrote:Any example? I did try to add placement property to a PythonFeature (DocumentObject) and watch for changes but could not get it to work. So I must have missed something.
DocumentObject already has a Placement property, you shouldn't need to add one if your object is derived from DocumentObject. look for "obj.Placement" in Mod/Draft/Draft.py for examples.

For translation:
from DraftTools import msg,translate
msg(translate("draft","Draft.makeBSpline: not enough points\n"), 'error')

wf
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Image WB in Python

Post by wmayer »

DocumentObject already has a Placement property, you shouldn't need to add one if your object is derived from DocumentObject. look for "obj.Placement" in Mod/Draft/Draft.py for examples.
DocumentObject doesn't have a Placement but the directly derived class GeoFeature does have it.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Image WB in Python

Post by microelly2 »

Is there still some activity and newer results on this image2 wb?

Otherwise I would "merge" it into my reconstruction wb
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Image WB in Python

Post by triplus »

microelly2 wrote:Is there still some activity and newer results on this image2 wb?

Otherwise I would "merge" it into my reconstruction wb
Currently we have REEN and Image WB in master. That is a good thing we have them as not having them would be less good. It is true functionality they provide is limited and it had been like that for years. Therefore the next step is to improve that.

I don't know and i could be wrong but the work done by @JAndersM to go in Image WB and the work you done in "reconstruction wb" to go in REEN. That i guess are the sort of improvements that need to happen in the future.
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image WB in Python

Post by JAndersM »

microelly2 wrote: Wed Jun 01, 2016 9:55 am Is there still some activity and newer results on this image2 wb?

Otherwise I would "merge" it into my reconstruction wb
No no activity...
Made a small bugfix as I revisited the WB yesterday. Thats it for the last 2 years...
Post Reply