specify document in Arch make methods

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

specify document in Arch make methods

Post by bernd »

Just realized in Arch it is not possible to parse the document the object is created in if use Arch.makeSomeSmartArchObject(parameters). The active document is used for all Arch make methods.

Mhh IMHO in no document is given the active should be used. For robustness it would makes sense to parse the doc the object is created in, would it not?

ATM I need to explizit set the ActiveDocument with the doc object I would like object to be created in. If not I get a miss match if creating documents objects inside the document and closing the document in a loop.


cheers bernd
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: specify document in Arch make methods

Post by vocx »

bernd wrote: Mon Sep 14, 2020 7:18 am ... For robustness it would makes sense to parse the doc the object is created in, would it not?
...
Do you mean something like this?

Code: Select all

doc1 = App.openDocument("Doc1")
doc2 = App.openDocument("Doc2")

Arch.make_something(doc1, args)
Arch.make_something(doc2, args)
I have thought about this as well, however, no code that I've seen in Draft, Arch, or BIM does it. It always assumes the active document.

So, changing this would mean refactoring the entire programming interface which would take a while, and could potentially break things.

The easiest thing that I can think about is adding an additional argument at the end to consider the document.

Code: Select all

def make_something(args, doc=None):
    if not doc:
        doc = App.activeDocument()
Last edited by vocx on Tue Sep 15, 2020 4:02 pm, edited 1 time in total.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: specify document in Arch make methods

Post by bernd »

exactly.

In FEM all make methods have a document parameter. In FEM it is at the end, which is cumbersome if you would like to use the active document, because you need to add it all the time. But IMHO better than no doc parameter.

I am curious what others think about this.
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: specify document in Arch make methods

Post by vanuan »

You're reading my mind :)

Yes, this is very much needed. For compatibility reasons, you have to make the document optional, but in reality, you can't create objects outside the document, can you? C++ defines "addObject" function as a method of a Document.

Maybe similarly Python should have it's own DocumentProxy?

E.g.

Code: Select all

class DocumentProxy:
  def __init__(self, doc):
      if not doc:
        raise Error("No active document. Aborting")
    self.doc = doc
  def addObject(self, proxy_class, arguments, name, cpp_type = 'Part::FeaturePython'):
    obj = self.doc.addObject(cpp_type, name)
    proxy_obj = proxy_class(obj)
    obj.Proxy = proxy_obj
    return obj

def makeWall(doc, *args):
  wall = doc.addObject(_Wall, args, 'wall', )
but maybe that's complicated. A simple additional parameter would do.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: specify document in Arch make methods

Post by bernd »

vanuan wrote: Wed Sep 16, 2020 7:54 am Yes, this is very much needed. For compatibility reasons, you have to make the document optional, but in reality, you can't create objects outside the document, can you?
FYI: you can not create document objects outside of a document, but in Part one can create shape objects without the need to create a document object. Arch, Draft and most FEM objects do not support this either. But this is a totally different story even more complicated to change.
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: specify document in Arch make methods

Post by yorik »

I agree with that as well. vocx's solution seems the easiest, to maintain backwards compatibility...
Post Reply