Implementing a FreeCAD Parts Library?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Implementing a FreeCAD Parts Library?

Postby cloudform511 » Thu Sep 22, 2011 11:45 pm

I've been dinking around with the Python interpreter and looking at the source code, and I think the only (serious) flaw with FreeCAD is that there is no parts library! Who is going to use a library if it doesn't have standard screws and screw holes, it would be a pain in the *$% to come up with it on your own... and then after you put in all that time, you can't even share it (easily)! That's no fun!

The object oriented approach that is fundamental to FreeCAD is such a stellar idea that I can't believe someone hasn't done this already. I would imagine it is (almost) simple to implement, without any changes to architecture!

I have two main goals:

- Create a "parts library" module. The parts would be designed using any of the standard Modules (Parts, Mesh, Drawing, etc), and could then be uploaded to a library. From the library you could embed a part in a project. You can also set it so that changes in the library change the part. You can even make "dynamic" objects that alter their shape from user input (they are wrapped in a python object). These things are PRACTICALLY already there (you could, for instance, pickle objects into a library so you could easily access them via scripting), there just isn't any UI to do so!

- Once in the library, parts can be shared into a "community library" with the click of a button. If the part name does not exist (in the community library), then it will create a new part. If the part name exist (or if it is too common, such as 'shape', 'thing', 'mypart', etc.) it will ask you to rename it, or "submit revision" to the existing part. When a user is browsing the community parts library, they can vote on which revision is the most accurate/useful. Obviously the parts with the most votes appear on top. Additionally, mods can go through parts and clear out bad ones or recommend goods ones.

Obviously this idea has two hurdles, and I'm going to put as much as I can into the library component before I touch the community sharing mechanism -- but if they could both be implemented, then every user could have an entire community library at their fingertips -- which is the true power of open source (community sharing).

Has anyone tried to do this? Is there a current project to accomplish either of these goals?

The first thing I am going to do is read the source of the Draft module (which, thank God, is in pure python), and replicate it into my own module. Any other suggestions?
User avatar
NormandC
Posts: 18534
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: Implementing a FreeCAD Parts Library?

Postby NormandC » Fri Sep 23, 2011 12:40 am

Hi,

Good idea. But I believe you should discuss this with jriegel, I think he had similar plans. He's away for a few days, he should be back by middle of next week.
mrlukeparry
Posts: 655
Joined: Fri Jul 22, 2011 8:37 pm
Contact:

Re: Implementing a FreeCAD Parts Library?

Postby mrlukeparry » Fri Sep 23, 2011 6:41 am

I've been dinking around with the Python interpreter and looking at the source code, and I think the only (serious) flaw with FreeCAD is that there is no parts library! Who is going to use a library if it doesn't have standard screws and screw holes, it would be a pain in the *$% to come up with it on your own... and then after you put in all that time, you can't even share it (easily)! That's no fun!

The object oriented approach that is fundamental to FreeCAD is such a stellar idea that I can't believe someone hasn't done this already. I would imagine it is (almost) simple to implement, without any changes to architecture!

I have two main goals:

- Create a "parts library" module. The parts would be designed using any of the standard Modules (Parts, Mesh, Drawing, etc), and could then be uploaded to a library. From the library you could embed a part in a project. You can also set it so that changes in the library change the part. You can even make "dynamic" objects that alter their shape from user input (they are wrapped in a python object). These things are PRACTICALLY already there (you could, for instance, pickle objects into a library so you could easily access them via scripting), there just isn't any UI to do so!

- Once in the library, parts can be shared into a "community library" with the click of a button. If the part name does not exist (in the community library), then it will create a new part. If the part name exist (or if it is too common, such as 'shape', 'thing', 'mypart', etc.) it will ask you to rename it, or "submit revision" to the existing part. When a user is browsing the community parts library, they can vote on which revision is the most accurate/useful. Obviously the parts with the most votes appear on top. Additionally, mods can go through parts and clear out bad ones or recommend goods ones.

Obviously this idea has two hurdles, and I'm going to put as much as I can into the library component before I touch the community sharing mechanism -- but if they could both be implemented, then every user could have an entire community library at their fingertips -- which is the true power of open source (community sharing).

Has anyone tried to do this? Is there a current project to accomplish either of these goals?

The first thing I am going to do is read the source of the Draft module (which, thank God, is in pure python), and replicate it into my own module. Any other suggestions?
I like the idea of open sharing, it seems a good idea and I would find probably find it useful. It would probably need a mechanism to add 'tags' to classify the object for searches. The community aspect such as ratings would require new infrastructure + I would suspect a web host. Also revisions would probably require a SCM such as GIT / SVN. Integrating the GUI directly into Free-cad would also be a bit time consuming and currently I think we have priorities to concentrate on the core mechanical CAD features at the moment and getting ready for 0.12 release. One thing that could maybe be done in the short term is create a default free-cad library that has essential parts library included .

BTW I hope I'm not being too negative. I'd suggest to try your idea in python. But just wait till jriegel is back.

Just an open question to everyone, what copyright/copyleft might the user use for CAD documents. I am curious to know if there would be any difference from the typical software licenses ?
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Re: Implementing a FreeCAD Parts Library?

Postby cloudform511 » Fri Sep 23, 2011 5:10 pm

mrlukeparry wrote: BTW I hope I'm not being too negative. I'd suggest to try your idea in python. But just wait till jriegel is back.
Not at all, all of these are realistic concerns. Especially the community aspect part, which would require all of the things you mention. However, the reason programs such as solidworks will continually be better than an open source project is because of their enourmous libraries -- companies actually create models of their own parts so that solid-works users can use them. The only way to rival this that I can see is to have an "intelligent" community library.

You are certainly right though. Focusing on the main system (the actual modeling) is very, very important; which is why it was my intent to work out these kinks (largely) on my own while you guys focused on the program itself. Then when I have a working plugin, I will submit it.
Integrating the GUI directly into Free-cad would also be a bit time consuming and currently I think we have priorities to concentrate on the core mechanical CAD features at the moment and getting ready for 0.12 release. One thing that could maybe be done in the short term is create a default free-cad library that has essential parts library included .
I'm thinking that the library won't be integrated directly into the ui (i.e. not a toolbar), but will instead be a separate qt window. The only integration will be a right-click menu to "add selection to library." Looking at the structure and source from other Mods, this looks like it won't be too difficult.

At first the library will just be a way to store objects or wrappers you created. If you want to change an object, you will have to bring it to an empty workbench, edit it, and then commit the revision to your personal library. Then you could delete the old one. The idea is that it will add a lot of functionality with as little complication as possible. Just an interface to store shapes and wrappers outside of individual projects.

A quick question, does anyone know how to run python scripts from within FreeCAD? Interactive interpreters are cool, but sometimes I want to run a script. I've tried to use the macro editor but I can't even get "hello world" to work on it.

Thanks!
stemby
Posts: 26
Joined: Wed Mar 30, 2011 4:53 pm
Location: Lombardy, Italy

Re: Implementing a FreeCAD Parts Library?

Postby stemby » Sat Sep 24, 2011 10:13 am

mrlukeparry wrote: Just an open question to everyone, what copyright/copyleft might the user use for CAD documents.
I think a license between CC BY, CC BY-SA or CC0/PD would be good.

http://creativecommons.org/licenses/
http://creativecommons.org/publicdomain/

Ciao!
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Re: Implementing a FreeCAD Parts Library?

Postby cloudform511 » Sat Sep 24, 2011 7:25 pm

double post
Last edited by cloudform511 on Sun Sep 25, 2011 10:43 pm, edited 2 times in total.
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Re: Implementing a FreeCAD Parts Library?

Postby cloudform511 » Sun Sep 25, 2011 1:18 am

Edit:

See my next post. Probably don't read this one.

I've made some progress.

I know I can get the brep file by:
App.ActiveDocument.myObject.Shape.exportBrep(filename)
Then I can take that data and write it into my xml library file.

Now I just have to figure out how to take data from my library file and turn it into a shape... I'm looking at it now.

# End Edit

Ok, so I think the best way to implement this would be to save the xml representations of objects (I am going to use the lxml library and save them into xml documents) and then load them from library. However, I am having some difficulty finding how to reload them into an active object.

Two (should be brief) questions:
1. Does the data from App.ActiveDocument.myObject.Content provide you with all you need to reconstruct an object exactly as it was? This includes if it is a simple object (lines, faces, cubes), or a more complicated object (involving Cut operations for instance). If not, is there a simple method to get the the necessary data?

2. What method can I use to reconstruct objects?

I assume these both exist, considering that FreeCAD has a way to save and load it's own filetypes! The documentation mentioned that .fcstd files were just "zipped xml files", so this should hopefully be pretty painless!

If anyone knows this it would be much appreciated! Thanks!
Last edited by cloudform511 on Sun Sep 25, 2011 2:13 am, edited 1 time in total.
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Re: Implementing a FreeCAD Parts Library?

Postby cloudform511 » Sun Sep 25, 2011 1:28 am

I have delved into the code a little more, and see some flaws in my thinking.

I can see that things can be exported with Shape.exportBrep(filename). However, it looks like entire objects are only stored via linking the .xml file with the .brep file (both of which are in the zipped .fcstd folder).

If I could get a pointer towards the source code that accomplishes the importing of objects from fcstd folders, and the exporting+linking, that would be fantastic and much appreciated. I will then create the library module using a similar architecture.

Thanks!

Edit: this is turning into kind of a log of me learning this library, which is not what I want, but I see that FreeCAD.loadFile is the function I am looking for. I'm trying to dig into the source now.
cloudform511
Posts: 17
Joined: Thu Sep 22, 2011 1:33 am

Re: Implementing a FreeCAD Parts Library?

Postby cloudform511 » Sun Sep 25, 2011 11:08 pm

I have figured most of it out!

Objects can be reconstructed from Object.Type and Object.Content by just calling their Properties (as stored in their Document.xml)

The exception, of course, comes with the objects that have to link, such as Cutting or Intersecting. For example, to make an object of a Box cut from a sphere (where Box and Sphere are already defined)

App.activeDocument().addObject("Part::Cut","Cut")
App.activeDocument().Cut.Base = Sphere
App.activeDocument().Cut.Tool = Box

To make an intersection:
App.activeDocument().addObject("Part::MultiCommon","Common")
App.activeDocument().Common.Shapes = [App.activeDocument().Box,App.activeDocument().Sphere]

of course, anyone can find this out easily enough if they just do the operations in FreeCAD with "Show script commands in Python" activated! (what an amazing feature).

Ok, so this is all very cool, and you guys obviously did an excellent job putting this whole thing together so it is easy to understand. I'm thinking there is a general process that can be run to construct any shape from it's xml file (assuming you also have the shapes it is linked to).

- If there is no pythonic/extendable implementation of this, I can start writing it.
- However, if there are functions or other files which are of use to me, I would like to know about them!

It seems to me that my first effort should be to simply create a function that can "import single part from fcstd"! If this does not exist, I volunteer to write it. It will take a little while, but it would be well worth the effort! Not only would we make it easy to extend it to then create libraries, but we would make it easier to import single objects from other projects!
logari81
Posts: 654
Joined: Mon Jun 14, 2010 6:00 pm

Re: Implementing a FreeCAD Parts Library?

Postby logari81 » Mon Sep 26, 2011 6:57 am

Hi, I also believe that a Parts Library is important.

I haven't understood how exactly you would like to implement it. Would you like to put all parts in one single fcstd file? Actually what we need is one fcstd per part. For example we can have one fcstd file for socket head screws of the same type and one dimensions table. The only thing needed by the user is a method to extract the dimensions from the table and apply them to the parametric socket head screw part. Before investing more time in this please wait for jriegel to be back so that you can discuss your ideas with him. He will probably have a plan already for this already and it will be nice if you would help to implement that plan.

In any case what you are trying now is definitely useful, since it helps you to get familiar with FreeCAD.