Parsing different App::FeaturePython

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
keithsloan52
Posts: 883
Joined: Mon Feb 27, 2012 5:31 pm

Parsing different App::FeaturePython

Postby keithsloan52 » Mon Jul 15, 2019 4:30 pm

In scanning a document I am trying to distinguish between objects defined as different App::FeaturePython.

I have tried with

Code: Select all

def __init__(self, obj)
self.Type = 'MyType'
It did not like obj.Type

But cannot see how to successfully test.
openBrain
Posts: 1590
Joined: Fri Nov 09, 2018 5:38 pm

Re: Parsing different App::FeaturePython

Postby openBrain » Mon Jul 15, 2019 6:25 pm

Really not clear.
__init__ is the function to initialize a class instance...
Then using '=' sign makes an assignation and not a comparison...
Could you provide a FreeCAD file to test, and try to explain what you're (functionally) trying to reach ?
keithsloan52
Posts: 883
Joined: Mon Feb 27, 2012 5:31 pm

Re: Parsing different App::FeaturePython

Postby keithsloan52 » Tue Jul 16, 2019 7:22 am

I have a number of App::FeaturePython classes

This being typical

Code: Select all

class GDMLTriangular(GDMLcommon) :
   def __init__(self, obj, v1, v2, v3, vtype):
      obj.addProperty("App::PropertyString","v1","Triangular", \
              "v1").v1=v1
      obj.addProperty("App::PropertyString","v2","Triangular", \
              "v1").v2=v2
      obj.addProperty("App::PropertyString","v3","Triangular", \
              "v1").v3=v3
      obj.addProperty("App::PropertyEnumeration","vtype","Triangular","vtype")
      obj.vtype=["Absolute", "Relative"]
      obj.vtype=0
      self.Type = 'GDMLTriangular'
      self.Object = obj
      obj.Proxy = self
Having created such objects in the file I am now trying to scan for export
which works for the Part::FeaturePython ones but not App::FeaturePython

Code: Select all

def processObject(obj, addVolsFlag) :
    print("\nProcess Object")
    # return solid or boolean reference name
    # addVolsFlag = True then create Logical & Physical Volumes
    #             = False needed for booleans
    #ET.ElementTree(gdml).write("test9a", 'utf-8', True)
    while switch(obj.TypeId) :
    ...
    ...
    # This works for Part::FeaturePython but NOT for App::FeaturePythn
    if case("Part::FeaturePython"):
          print("   Python Feature")
          if hasattr(obj.Proxy, 'Type') :
             print(obj.Proxy.Type)
             switch(obj.Proxy.Type)
             if case("GDMLBox") :
             ....
             ....
      # attempt to handle App::FeaturePython which does NOT work
      # Same as Part::Feature but no position
      if case("App::FeaturePython") :
         print("App::FeaturePython")
         if hasattr(obj.Proxy, 'Type') :
            print(obj.Proxy.Type)
            switch(obj.Proxy.Type)
I use user define functions switch and case to make the code more readable and they are not a problem
wmayer
Site Admin
Posts: 14448
Joined: Thu Feb 19, 2009 10:32 am

Re: Parsing different App::FeaturePython

Postby wmayer » Tue Jul 16, 2019 9:10 am

You don't really need the Type attribute in your proxy classes. Instead you can use Python's type-system mechanism:

Code: Select all

if isinstance(obj.Proxy, GDMLTriangular):
keithsloan52
Posts: 883
Joined: Mon Feb 27, 2012 5:31 pm

Re: Parsing different App::FeaturePython

Postby keithsloan52 » Tue Jul 16, 2019 12:15 pm

wmayer wrote:
Tue Jul 16, 2019 9:10 am
You don't really need the Type attribute in your proxy classes. Instead you can use Python's type-system mechanism:

Code: Select all

if isinstance(obj.Proxy, GDMLTriangular):
Thanks