def parsePhysVol(parent,physVol,displayMode):
    GDMLShared.trace("ParsePhyVol")
    posref = GDMLShared.getRef(physVol,"positionref")
    if posref is not None :
       GDMLShared.trace("positionref : "+posref)
       pos = GDMLShared.define.find("position[@name='%s']" % posref )
       if pos is not None : GDMLShared.trace(pos.attrib)
    else :
       pos = physVol.find("position")
    if pos is not None :
       px = GDMLShared.getVal(pos,'x')
       py = GDMLShared.getVal(pos,'y')
       pz = GDMLShared.getVal(pos,'z')
    else :
       px = py = pz = 0 
    rotref = GDMLShared.getRef(physVol,"rotationref")
    if rotref is not None :
       rot = GDMLShared.define.find("rotation[@name='%s']" % rotref )
    else :
       rot = physVol.find("rotation")

    volref = GDMLShared.getRef(physVol,"volumeref")
    GDMLShared.trace("Volume ref : "+volref)
    part = parent.newObject("App::Part",volref)
    parseVolume(part,volref,px,py,pz,rot,displayMode)
def parseBoolean(part,solid,objType,material,px,py,pz,rot,displayMode) :
    from GDMLObjects import ViewProvider
    GDMLShared.trace(solid.tag)
    GDMLShared.trace(solid.attrib)
    if solid.tag in ["subtraction","union","intersection"] :
       GDMLShared.trace("Boolean : "+solid.tag)
       name1st = GDMLShared.getRef(solid,'first')
       base = solids.find("*[@name='%s']" % name1st )
       GDMLShared.trace("first : "+name1st)
       #parseObject(root,base)
       name2nd = GDMLShared.getRef(solid,'second')
       tool = solids.find("*[@name='%s']" % name2nd )
       GDMLShared.trace("second : "+name2nd)
       #parseObject(root,tool)
       #mybool = volObj.newObject(objType,solid.tag+':'+getName(solid))
       mybool = part.newObject(objType,solid.tag+':'+getName(solid))
       #mybool.Base = createSolid(volObj,base,material,0,0,0,None,displayMode)
       mybool.Base = createSolid(part,base,material,0,0,0,None,displayMode)
       #mybool.Base = createSolid(base,px,py,pz,rot)
       # second solid is placed at position and rotation relative to first
       mybool.Tool = createSolid(part,tool,material,0,0,0,None,displayMode)
       mybool.Tool.Placement= GDMLShared.getPlacementFromRefs(solid) 
       # Okay deal with position of boolean
       GDMLShared.trace("Position : "+str(px)+','+str(py)+','+str(pz))
       base = FreeCAD.Vector(0,0,0)
       #base = FreeCAD.Vector(px,py,pz)
       mybool.Placement = GDMLShared.processPlacement(base,rot)
       #ViewProvider(mybool.ViewObject)
       #print("Bool Shape : "+str(mybool.Shape.isValid()))
       #print("Bool Base  : "+str(mybool.Base.Shape.isValid()))
       #print("Bool Tool  : "+str(mybool.Tool.Shape.isValid()))
       print(dir(mybool.Shape))
       return mybool
def expandVolume(parent, name, px, py, pz, rot, phylvl, displayMode):
    import FreeCAD as App
    # also used in ScanCommand
    vol = structure.find("volume[@name='%s']" % name)
    if vol != None:  # If not volume test for assembly
        solidref = GDMLShared.getRef(vol, "solidref")
        if solidref != None:
            solid = solids.find("*[@name='%s']" % solidref)
            GDMLShared.trace(solid.tag)
            # Material is the materialref value
            # need to add default
            #material = GDMLShared.getRef(vol,"materialref")
            material = GDMLShared.getRef(vol, "materialref")
            #createSolid(part,solid,material,px,py,pz,rot,displayMode)
            #print('solid : '+solid.tag)
            #print('material :'+material)
            obj = createSolid(parent, solid, material, px, py, pz, rot,
                              displayMode)
        # Volume may or maynot contain physvol's
        displayMode = 1
        for pv in vol.findall("physvol"):
            # Need to clean up use of phylvl flag
            # create solids at pos & rot in physvols
            #if phylvl < 1 :
            if phylvl < 0:
                if phylvl >= 0:
                    phylvl += 1
                # If negative always parse otherwise increase level
                parsePhysVol(parent, pv, phylvl, displayMode)
            else:  # Just Add to structure
                from PySide import QtGui, QtCore
                volref = GDMLShared.getRef(pv, "volumeref")
                GDMLShared.trace("Volume ref : " + volref)
                #part = parent.newObject("App::Part","\033[1;40;31m"+volref)
                part = parent.newObject("App::Part", "NOT-Expanded_" + volref)
                #obj = part.newObject("App::Annotation","Not Expanded")
                #obj.LabelText="Annotation"
                #view = obj.ViewObject
                #print(dir(view))
                #part = parent.newObject("App::DocumentObjectGroup",volref)
                #vpart2 = part2.ViewObject
                #print(dir(vpart2))
                # 100% red, 0% Green, 0% Blue
                #vpart.TextColor = (100., 0., 0., 0.)
        # Add parsed Volume to dict
        volDict[name] = obj
        App.ActiveDocument.recompute()
        return obj

    else:
        asm = structure.find("assembly[@name='%s']" % name)
        print("Assembly : " + name)
        if asm != None:
            for pv in asm.findall("physvol"):
                # create solids at pos & rot in physvols
                #parsePhysVol(part,pv,displayMode)
                #obj = parent.newObject("App::Part",name)
                parsePhysVol(parent, pv, phylvl, displayMode)
        else:
            print("Not Volume or Assembly")
Beispiel #4
0
def parseVolume(parent, name, px, py, pz, rot, displayMode):
    global volDict

    # Has the volume already been parsed i.e in Assembly etc
    obj = volDict.get(name)
    if obj != None:
        newobj = Draft.clone(obj)
        #print(dir(newobj))
        #print(newobj.TypeId)
        #print(newobj.Name)
        #print(newobj.Label)
        parent.addObject(newobj)
        base = FreeCAD.Vector(px, py, pz)
        newobj.Placement = GDMLShared.processPlacement(base, rot)
        return

    else:
        GDMLShared.trace("ParseVolume : " + name)
        #part = parent.newObject("App::Part",name)
        vol = structure.find("volume[@name='%s']" % name)
        if vol != None:  # If not volume test for assembly
            solidref = GDMLShared.getRef(vol, "solidref")
            if solidref != None:
                solid = solids.find("*[@name='%s']" % solidref)
                GDMLShared.trace(solid.tag)
                # Material is the materialref value
                # need to add default
                #material = GDMLShared.getRef(vol,"materialref")
                material = GDMLShared.getRef(vol, "materialref")
                #createSolid(part,solid,material,px,py,pz,rot,displayMode)
                obj = createSolid(parent, solid, material, px, py, pz, rot,
                                  displayMode)
            # Volume may or maynot contain physvol's
            displayMode = 1
            for pv in vol.findall("physvol"):
                # create solids at pos & rot in physvols
                #parsePhysVol(part,pv,displayMode)
                #obj = parent.newObject("App::Part",name)
                parsePhysVol(parent, pv, displayMode)
            # Add parsed Volume to dict
            volDict[name] = obj
            return obj

        else:
            asm = structure.find("assembly[@name='%s']" % name)
            print("Assembly : " + name)
            if asm != None:
                for pv in asm.findall("physvol"):
                    # create solids at pos & rot in physvols
                    #parsePhysVol(part,pv,displayMode)
                    #obj = parent.newObject("App::Part",name)
                    parsePhysVol(parent, pv, displayMode)
            else:
                print("Not Volume or Assembly")
def getVolSolid(name):
    GDMLShared.trace("Get Volume Solid")
    vol = structure.find("/volume[@name='%s']" % name )
    sr = vol.find("solidref")
    GDMLShared.trace(sr.attrib)
    name = GDMLShared.getRef(sr)
    solid = solids.find("*[@name='%s']" % name )
    return solid
def processGDML(doc,filename):

    import GDMLShared
    import GDMLObjects

    params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/GDML")
    GDMLShared.printverbose = params.GetBool('printVerbose',False)
    print("Print Verbose : "+ str(GDMLShared.printverbose))

    FreeCAD.Console.PrintMessage('Import GDML file : '+filename+'\n')
    FreeCAD.Console.PrintMessage('ImportGDML Version 0.2\n')
    
    global pathName
    pathName = os.path.dirname(os.path.normpath(filename))
    FilesEntity = False

    global setup, materials, solids, structure, volDict
  
  # Add files object so user can change to organise files
  #  from GDMLObjects import GDMLFiles, ViewProvider
  #  myfiles = doc.addObject("App::FeaturePython","Export_Files")
    #myfiles = doc.addObject("App::DocumentObjectGroupPython","Export_Files")
    #GDMLFiles(myfiles,FilesEntity,sectionDict)

    from lxml import etree
    #root = etree.fromstring(currentString)
    parser = etree.XMLParser(resolve_entities=True)
    root = etree.parse(filename, parser=parser)

    setup     = root.find('setup')
    print("Call set Define")
    GDMLShared.setDefine(root.find('define'))
    materials = root.find('materials')
    solids    = root.find('solids')
    structure = root.find('structure')

    # volDict dictionary of volume names and associated FreeCAD part
    volDict = {}

    GDMLShared.processConstants(doc)
    GDMLShared.trace(setup.attrib)
    processIsotopes(doc)
    processElements(doc)
    processMaterials(doc)

    world = GDMLShared.getRef(setup,"world")
    #print(world)
    part =doc.addObject("App::Part",world)
    parseVolume(part,world,0,0,0,None,3)

    doc.recompute()
    FreeCADGui.SendMsgToActiveView("ViewFit")
    FreeCAD.Console.PrintMessage('End processing GDML file\n')