Guidance for creating new object in TD

Discussions about the development of the TechDraw workbench
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
walpa
Posts: 65
Joined: Wed Nov 09, 2016 5:22 pm
Location: Brazil

Guidance for creating new object in TD

Post by walpa »

Hi everybody,

I'm trying to get deeper into TD, and from what I've been able to test and understand:

- the object selected in the tree view is vectorized (according to type) in an svg and inserted as a child object in a TD object.
- does not work for App::FeaturePython objects, created with macro, and with its 'drawn' shape in the ViewProvider. (e.g.: macro sent)

Code: Select all

import FreeCAD
from pivy import coin

class Box:
    def __init__(self, obj):
        obj.addProperty("App::PropertyPlacement","Placement","Base","Placement")
        obj.addProperty("App::PropertyLength","Length","Box","Length of the box").Length=10.0
        obj.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=10.0
        obj.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=10.0
        obj.Proxy = self

class ViewProviderBox:
    def __init__(self, obj):
        obj.addProperty("App::PropertyInteger","Transparency","Box","Transparency").Transparency = 0
        obj.addProperty("App::PropertyColor","Color","Box","Color of the box").Color=(1.0,0.0,0.0)
        obj.Proxy = self
 
    def attach(self, obj):
        self.shaded = coin.SoGroup()
        self.wireframe = coin.SoGroup()
        self.flatlines = coin.SoGroup()
        self.scale = coin.SoScale()
        self.mat = coin.SoMaterial()
        self.trl = coin.SoTranslation()
        coords = coin.SoCoordinate3()
        
        self.data=coin.SoCube()
 
        self.shaded.addChild(self.scale)
        self.shaded.addChild(coords)
        self.shaded.addChild(self.mat)
        self.shaded.addChild(self.trl)
        self.shaded.addChild(self.data)
        obj.addDisplayMode(self.shaded,"Shaded")
        
        style1=coin.SoDrawStyle()
        style1.style = coin.SoDrawStyle.LINES
        self.wireframe.addChild(style1)
        self.wireframe.addChild(self.scale)
        self.wireframe.addChild(coords)
        self.wireframe.addChild(self.mat)
        self.wireframe.addChild(self.trl)
        self.wireframe.addChild(self.data)
        obj.addDisplayMode(self.wireframe,"Wireframe")
        
        self.flatlines.addChild(self.scale)
        self.flatlines.addChild(coords)
        self.flatlines.addChild(self.mat)
        self.flatlines.addChild(self.trl)
        self.flatlines.addChild(self.data)
        obj.addDisplayMode(self.flatlines,"FlatLines")

        self.onChanged(obj,"Color")
 
    def updateData(self, fp, prop):
        l = fp.getPropertyByName("Length")
        w = fp.getPropertyByName("Width")
        h = fp.getPropertyByName("Height")
        
        self.data.width = float(l)
        self.data.height = float(w)
        self.data.depth  = float(h)
        
        if prop == "Placement":
            p = fp.Placement.Base
            self.trl.translation=(p.x,p.y,p.z)
 
    def getDisplayModes(self,obj):
        '''Return a list of display modes.'''
        modes=[]
        modes.append("Shaded")
        modes.append("Wireframe")
        modes.append("FlatLines")
        return modes
 
    def getDefaultDisplayMode(self):
        return "FlatLines"
 
    def setDisplayMode(self,mode):
        return mode
 
    def onChanged(self, vp, prop):
        if prop == "Color":
            c = vp.getPropertyByName("Color")
            self.mat.diffuseColor = (c[0], c[1], c[2])
            self.mat.emissiveColor = (c[0], c[1], c[2])
        if prop == "Transparency":
            t = float(vp.Transparency/100)
            self.mat.transparency.setValue(t)
 
    def __getstate__(self):
        return None
 
    def __setstate__(self,state):
        return None

def makeBox():
    a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box")
    Box(a)
    ViewProviderBox(a.ViewObject)

makeBox()
FreeCAD.ActiveDocument.recompute()

Am I right?

My questions:
To create a TD object using an object created like the one in the example, would I have to render the scenario graph diagram and convert it to an svg file?
Is there another way to do this?

Thank for advance for your attention and help.
Post Reply