diff --git a/src/Mod/Arch/InitGui.py b/src/Mod/Arch/InitGui.py index a2e7a96a01..a30c752c39 100644 --- a/src/Mod/Arch/InitGui.py +++ b/src/Mod/Arch/InitGui.py @@ -79,7 +79,7 @@ class ArchWorkbench(Workbench): "Draft_SelectGroup","Draft_SelectPlane", "Draft_ShowSnapBar","Draft_ToggleGrid","Draft_UndoLine", "Draft_FinishLine","Draft_CloseLine"] - self.draftutils = ["Draft_VisGroup","Draft_Heal","Draft_FlipDimension", + self.draftutils = ["Draft_Layer","Draft_Heal","Draft_FlipDimension", "Draft_ToggleConstructionMode","Draft_ToggleContinueMode","Draft_Edit", "Draft_Slope","Draft_SetWorkingPlaneProxy","Draft_AddConstruction"] self.snapList = ['Draft_Snap_Lock','Draft_Snap_Midpoint','Draft_Snap_Perpendicular', diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index 01d1277563..0b1b650608 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -14,6 +14,7 @@ SET(Draft_SRCS DraftTrackers.py DraftVecUtils.py DraftGeomUtils.py + DraftLayer.py WorkingPlane.py getSVG.py importDXF.py diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index de2c5cd9fd..4e7d05ab9d 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -67,6 +67,15 @@ def translate(ctx,txt): arrowtypes = ["Dot","Circle","Arrow","Tick","Tick-2"] +#--------------------------------------------------------------------------- +# Backwards compatibility +#--------------------------------------------------------------------------- + +import DraftLayer +_VisGroup = DraftLayer.Layer +_ViewProviderVisGroup = DraftLayer.ViewProviderLayer +makeLayer = DraftLayer.makeLayer + #--------------------------------------------------------------------------- # General functions #--------------------------------------------------------------------------- @@ -123,6 +132,8 @@ def getParam(param,default=None): if t == "int": if default == None: default = 0 + if param == "linewidth": + return FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetInt("DefaultShapeLineWidth",default) return p.GetInt(param,default) elif t == "string": if default == None: @@ -150,7 +161,10 @@ def setParam(param,value): p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") t = getParamType(param) if t == "int": - p.SetInt(param,value) + if param == "linewidth": + FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").SetInt("DefaultShapeLineWidth",value) + else: + p.SetInt(param,value) elif t == "string": p.SetString(param,value) elif t == "float": @@ -1338,21 +1352,6 @@ def makeEllipse(majradius,minradius,placement=None,face=True,support=None): return obj -def makeVisGroup(group=None,name="VisGroup"): - '''makeVisGroup([group]): creates a VisGroup object in the given group, or in the - active document if no group is given''' - if not FreeCAD.ActiveDocument: - FreeCAD.Console.PrintError("No active document. Aborting\n") - return - obj = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroupPython",name) - _VisGroup(obj) - if FreeCAD.GuiUp: - _ViewProviderVisGroup(obj.ViewObject) - formatObject(obj) - if group: - group.addObject(obj) - return obj - def extrude(obj,vector,solid=False): '''makeExtrusion(object,vector): extrudes the given object in the direction given by the vector. The original object @@ -6275,82 +6274,6 @@ class _ViewProviderFacebinder(_ViewProviderDraft): return False -class _VisGroup: - "The VisGroup object" - def __init__(self,obj): - self.Type = "VisGroup" - obj.Proxy = self - self.Object = obj - - def __getstate__(self): - return self.Type - - def __setstate__(self,state): - if state: - self.Type = state - - def execute(self,obj): - pass - -class _ViewProviderVisGroup: - "A View Provider for the VisGroup object" - def __init__(self,vobj): - vobj.addProperty("App::PropertyColor","LineColor","Base","") - vobj.addProperty("App::PropertyColor","ShapeColor","Base","") - vobj.addProperty("App::PropertyFloat","LineWidth","Base","") - vobj.addProperty("App::PropertyEnumeration","DrawStyle","Base","") - vobj.addProperty("App::PropertyInteger","Transparency","Base","") - vobj.DrawStyle = ["Solid","Dashed","Dotted","Dashdot"] - vobj.LineWidth = 1 - vobj.LineColor = (0.13,0.15,0.37) - vobj.DrawStyle = "Solid" - vobj.Proxy = self - - def getIcon(self): - import Arch_rc - return ":/icons/Draft_VisGroup.svg" - - def attach(self,vobj): - self.Object = vobj.Object - return - - def claimChildren(self): - return self.Object.Group - - def __getstate__(self): - return None - - def __setstate__(self,state): - return None - - def updateData(self,obj,prop): - if prop == "Group": - if obj.ViewObject: - obj.ViewObject.Proxy.onChanged(obj.ViewObject,"LineColor") - - def onChanged(self,vobj,prop): - if hasattr(vobj,"Object"): - if vobj.Object: - if hasattr(vobj.Object,"Group"): - if vobj.Object.Group: - for o in vobj.Object.Group: - if o.ViewObject: - for p in ["LineColor","ShapeColor","LineWidth","DrawStyle","Transparency"]: - if hasattr(vobj,p): - if hasattr(o.ViewObject,p): - setattr(o.ViewObject,p,getattr(vobj,p)) - elif hasattr(o,p): - # for Drawing views - setattr(o,p,getattr(vobj,p)) - elif (p == "DrawStyle") and hasattr(o,"LineStyle"): - # Special case in Drawing views - setattr(o,"LineStyle",getattr(vobj,p)) - if vobj.Object.InList: - # touch the page if something was changed - if vobj.Object.InList[0].isDerivedFrom("Drawing::FeaturePage"): - vobj.Object.InList[0].touch() - - class WorkingPlaneProxy: "The Draft working plane proxy object" diff --git a/src/Mod/Draft/DraftGui.py b/src/Mod/Draft/DraftGui.py index c5e4dcc09c..ad2bdc9b12 100644 --- a/src/Mod/Draft/DraftGui.py +++ b/src/Mod/Draft/DraftGui.py @@ -1958,7 +1958,7 @@ class DraftToolBar: g = float(self.color.green()/255.0) b = float(self.color.blue()/255.0) elif type == "face": - color = facecolor = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetUnsigned("DefaultShapeColor",4294967295) + color = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetUnsigned("DefaultShapeColor",4294967295) r = ((color>>24)&0xFF)/255 g = ((color>>16)&0xFF)/255 b = ((color>>8)&0xFF)/255 diff --git a/src/Mod/Draft/DraftLayer.py b/src/Mod/Draft/DraftLayer.py new file mode 100644 index 0000000000..abc6fd42c5 --- /dev/null +++ b/src/Mod/Draft/DraftLayer.py @@ -0,0 +1,369 @@ +# -*- coding: utf-8 -*- + +#*************************************************************************** +#* * +#* Copyright (c) 2009, 2010 * +#* Yorik van Havre , Ken Cline * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import FreeCAD + +def translate(ctx,txt): + return txt + +def QT_TRANSLATE_NOOP(ctx,txt): + return txt + +"This module contains everything related to Draft Layers" + + +def makeLayer(name=None): + + '''makeLayer([name]): creates a Layer object in the active document''' + + if not FreeCAD.ActiveDocument: + FreeCAD.Console.PrintError(translate("draft","No active document. Aborting")+"\n") + return + obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython","Layer") + Layer(obj) + if name: + obj.Label = name + else: + obj.Label = translate("draft","Layer") + if FreeCAD.GuiUp: + ViewProviderLayer(obj.ViewObject) + getLayerContainer().addObject(obj) + return obj + + +def getLayerContainer(): + + '''getLayerContainer(): returns a group object to put layers in''' + + for obj in FreeCAD.ActiveDocument.Objects: + if obj.Name == "LayerContainer": + return obj + obj = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroupPython","LayerContainer") + obj.Label = translate("draft","Layers") + LayerContainer(obj) + if FreeCAD.GuiUp: + ViewProviderLayerContainer(obj.ViewObject) + return obj + + +class CommandLayer(): + + "The Draft_Layer FreeCAD command" + + def GetResources(self): + + return {'Pixmap' : 'Draft_VisGroup', + 'MenuText': QT_TRANSLATE_NOOP("Draft_Layer", "Layer"), + 'ToolTip' : QT_TRANSLATE_NOOP("Draft_Layer", "Adds a layer")} + + def Activated(self): + + import FreeCADGui + FreeCAD.ActiveDocument.openTransaction("Create Layer") + FreeCADGui.addModule("Draft") + FreeCADGui.doCommand('Draft.makeLayer()') + FreeCADGui.doCommand('FreeCAD.ActiveDocument.recompute()') + FreeCAD.ActiveDocument.commitTransaction() + FreeCAD.ActiveDocument.recompute() + + +class Layer: + + "The Draft Layer object" + + def __init__(self,obj): + + self.Type = "Layer" + obj.Proxy = self + self.Object = obj + self.setProperties(obj) + + def onDocumentRestored(self,obj): + + self.setProperties(obj) + + def setProperties(self,obj): + + if not "Group" in obj.PropertiesList: + obj.addProperty("App::PropertyLinkList","Group","Layer",QT_TRANSLATE_NOOP("App::Property","The objects that are part of this layer")) + + def __getstate__(self): + + return self.Type + + def __setstate__(self,state): + + if state: + self.Type = state + + def execute(self,obj): + pass + + +class ViewProviderLayer: + + "A View Provider for the Layer object" + + def __init__(self,vobj): + + vobj.addProperty("App::PropertyBool","OverrideChildren","Layer",QT_TRANSLATE_NOOP("App::Property","If on, the child objects of this layer will match its visual aspects")) + vobj.addProperty("App::PropertyColor","LineColor","Layer",QT_TRANSLATE_NOOP("App::Property","The line color of the children of this layer")) + vobj.addProperty("App::PropertyColor","ShapeColor","Layer",QT_TRANSLATE_NOOP("App::Property","The shape color of the children of this layer")) + vobj.addProperty("App::PropertyFloat","LineWidth","Layer",QT_TRANSLATE_NOOP("App::Property","The line width of the children of this layer")) + vobj.addProperty("App::PropertyEnumeration","DrawStyle","Layer",QT_TRANSLATE_NOOP("App::Property","The draw style of the children of this layer")) + vobj.addProperty("App::PropertyInteger","Transparency","Layer",QT_TRANSLATE_NOOP("App::Property","The transparency of the children of this layer")) + vobj.DrawStyle = ["Solid","Dashed","Dotted","Dashdot"] + + vobj.OverrideChildren = True + c = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetUnsigned("DefaultShapeLineColor",255) + vobj.LineColor = (((c>>24)&0xFF)/255,((c>>16)&0xFF)/255,((c>>8)&0xFF)/255) + w = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetInt("DefaultShapeLineWidth",2) + vobj.LineWidth = w + c = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/View").GetUnsigned("DefaultShapeColor",4294967295) + vobj.ShapeColor = (((c>>24)&0xFF)/255,((c>>16)&0xFF)/255,((c>>8)&0xFF)/255) + vobj.DrawStyle = "Solid" + + vobj.Proxy = self + + def getIcon(self): + + if hasattr(self,"icondata"): + return self.icondata + import Draft_rc + return ":/icons/Draft_VisGroup.svg" + + def attach(self,vobj): + + self.Object = vobj.Object + from pivy import coin + sep = coin.SoGroup() + vobj.addDisplayMode(sep,"Default") + return + + def claimChildren(self): + + if hasattr(self,"Object") and hasattr(self.Object,"Group"): + return self.Object.Group + + def getDisplayModes(self, vobj): + + return ["Default"] + + def getDefaultDisplayMode(self): + + return "Default" + + def setDisplayMode(self, mode): + + return mode + + def __getstate__(self): + + return None + + def __setstate__(self,state): + + return None + + def updateData(self,obj,prop): + + if prop == "Group": + self.onChanged(obj.ViewObject,"LineColor") + + def onChanged(self,vobj,prop): + + if hasattr(vobj,"OverrideChildren") and vobj.OverrideChildren: + if hasattr(vobj,"Object")and hasattr(vobj.Object,"Group"): + for o in vobj.Object.Group: + if o.ViewObject: + for p in ["LineColor","ShapeColor","LineWidth","DrawStyle","Transparency"]: + if hasattr(vobj,p) and hasattr(o.ViewObject,p): + setattr(o.ViewObject,p,getattr(vobj,p)) + + if (prop == "Visibility") and hasattr(vobj,"Visibility"): + if hasattr(vobj,"Object")and hasattr(vobj.Object,"Group"): + for o in vobj.Object.Group: + if o.ViewObject and hasattr(o.ViewObject,"Visibility"): + o.ViewObject.Visibility = vobj.Visibility + + if (prop in ["LineColor","ShapeColor"]) and hasattr(vobj,"LineColor") and hasattr(vobj,"ShapeColor"): + from PySide import QtCore,QtGui + lc = vobj.LineColor + sc = vobj.ShapeColor + lc = QtGui.QColor(int(lc[0]*255),int(lc[1]*255),int(lc[2]*255)) + sc = QtGui.QColor(int(sc[0]*255),int(sc[1]*255),int(sc[2]*255)) + p1 = QtCore.QPointF(2,17) + p2 = QtCore.QPointF(13,8) + p3 = QtCore.QPointF(30,15) + p4 = QtCore.QPointF(20,25) + im = QtGui.QImage(32,32,QtGui.QImage.Format_ARGB32) + im.fill(QtCore.Qt.transparent) + pt = QtGui.QPainter(im) + pt.setBrush(QtGui.QBrush(sc, QtCore.Qt.SolidPattern)) + pt.drawPolygon([p1,p2,p3,p4]) + pt.setPen(QtGui.QPen(lc, 2, QtCore.Qt.SolidLine, QtCore.Qt.FlatCap)) + pt.drawPolygon([p1,p2,p3,p4]) + pt.end() + ba = QtCore.QByteArray() + b = QtCore.QBuffer(ba) + b.open(QtCore.QIODevice.WriteOnly) + im.save(b,"XPM") + self.icondata = ba.data().decode("latin1") + + def canDragObject(self,obj): + + return True + + def canDragObjects(self): + + return True + + def dragObject(self,vobj,otherobj): + + if hasattr(vobj.Object,"Group"): + if otherobj in vobj.Object.Group: + g = vobj.Object.Group + g.remove(otherobj) + vobj.Object.Group = g + FreeCAD.ActiveDocument.recompute() + + def canDropObject(self,obj): + + if hasattr(obj,"Proxy") and isinstance(obj.Proxy,Layer): # for now, prevent stacking layers + return False + return True + + def canDropObjects(self): + + return True + + def dropObject(self,vobj,otherobj): + + if hasattr(vobj.Object,"Group"): + if not otherobj in vobj.Object.Group: + if not(hasattr(otherobj,"Proxy") and isinstance(otherobj.Proxy,Layer)): # for now, prevent stacking layers + g = vobj.Object.Group + g.append(otherobj) + vobj.Object.Group = g + # remove from all other layers (not automatic) + for parent in otherobj.InList: + if hasattr(parent,"Proxy") and isinstance(parent.Proxy,Layer): + if otherobj in parent.Group: + if parent != vobj.Object: + g = parent.Group + g.remove(otherobj) + parent.Group = g + FreeCAD.ActiveDocument.recompute() + + +class LayerContainer: + + "The Layer Container" + + def __init__(self,obj): + + self.Type = "LayerContainer" + obj.Proxy = self + + def execute(self,obj): + + return + + def __getstate__(self): + + if hasattr(self,"Type"): + return self.Type + + def __setstate__(self,state): + + if state: + self.Type = state + + +class ViewProviderLayerContainer: + + "A View Provider for the Layer Container" + + def __init__(self,vobj): + + vobj.Proxy = self + + def getIcon(self): + + import Draft_rc + return ":/icons/Draft_VisGroup.svg" + + def attach(self,vobj): + + self.Object = vobj.Object + + def setupContextMenu(self,vobj,menu): + + import Draft_rc + from PySide import QtCore,QtGui + action1 = QtGui.QAction(QtGui.QIcon(":/icons/Draft_VisGroup.svg"),"Merge duplicates",menu) + action1.triggered.connect(self.mergeByName) + menu.addAction(action1) + + def mergeByName(self): + + if hasattr(self,"Object") and hasattr(self.Object,"Group"): + layers = [o for o in self.Object.Group if (hasattr(o,"Proxy") and isinstance(otherobj.Proxy,Layer))] + todelete = [] + for layer in layerss: + if layer.Label[-1].isdigit() and layer.Label[-2].isdigit() and layer.Label[-3].isdigit(): + orig = None + for ol in layer: + if ol.Label == layer.Label[:-3].strip(): + orig = ol + break + if orig: + for par in layer.InList: + for prop in par.PropertiesList: + if getattr(par,prop) == layer: + FreeCAD.Console.PrintMessage("Changed property '"+prop+"' of object "+par.Label+" from "+layer.Label+" to "+orig.Label+"\n") + setattr(par,prop,orig) + todelete.append(layer) + for tod in todelete: + if not tod.InList: + FreeCAD.Console.PrintMessage("Merging duplicate layer "+tod.Label+"\n") + FreeCAD.ActiveDocument.removeObject(tod.Name) + elif (len(tod.InList) == 1) and (tod.InList[0].isDerivedFrom("App::DocumentObjectGroup")): + FreeCAD.Console.PrintMessage("Merging duplicate layer "+tod.Label+"\n") + FreeCAD.ActiveDocument.removeObject(tod.Name) + else: + FreeCAD.Console.PrintMessage("Unable to delete layer "+tod.Label+": InList not empty\n") + + def __getstate__(self): + + return None + + def __setstate__(self,state): + + return None + +if FreeCAD.GuiUp: + import FreeCADGui + FreeCADGui.addCommand('Draft_Layer',CommandLayer()) diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 5fca0e91bc..c4c2f9698a 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -5971,31 +5971,6 @@ class Draft_FlipDimension(): FreeCAD.ActiveDocument.recompute() -class VisGroup(): - def GetResources(self): - return {'Pixmap' : 'Draft_VisGroup', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_VisGroup", "VisGroup"), - 'ToolTip' : QtCore.QT_TRANSLATE_NOOP("Draft_VisGroup", "Adds a VisGroup")} - - def Activated(self): - s = FreeCADGui.Selection.getSelection() - FreeCAD.ActiveDocument.openTransaction("Create VisGroup") - FreeCADGui.addModule("Draft") - if len(s) == 1: - if s[0].isDerivedFrom("App::DocumentObjectGroup"): - FreeCADGui.doCommand("v = Draft.makeVisGroup(FreeCAD.ActiveDocument."+s[0].Name+")") - FreeCADGui.doCommand('Draft.autogroup(v)') - FreeCADGui.doCommand('FreeCAD.ActiveDocument.recompute()') - FreeCAD.ActiveDocument.commitTransaction() - FreeCAD.ActiveDocument.recompute() - return - FreeCADGui.doCommand("v = Draft.makeVisGroup()") - FreeCADGui.doCommand('Draft.autogroup(v)') - FreeCADGui.doCommand('FreeCAD.ActiveDocument.recompute()') - FreeCAD.ActiveDocument.commitTransaction() - FreeCAD.ActiveDocument.recompute() - - class Mirror(Modifier): "The Draft_Mirror FreeCAD command definition" @@ -6723,7 +6698,6 @@ FreeCADGui.addCommand('Draft_Clone',Draft_Clone()) FreeCADGui.addCommand('Draft_PathArray',PathArray()) FreeCADGui.addCommand('Draft_PointArray',PointArray()) FreeCADGui.addCommand('Draft_Heal',Heal()) -FreeCADGui.addCommand('Draft_VisGroup',VisGroup()) FreeCADGui.addCommand('Draft_Mirror',Mirror()) FreeCADGui.addCommand('Draft_Slope',Draft_Slope()) FreeCADGui.addCommand('Draft_Stretch',Stretch()) diff --git a/src/Mod/Draft/InitGui.py b/src/Mod/Draft/InitGui.py index f7ed639cf8..1a669897f5 100644 --- a/src/Mod/Draft/InitGui.py +++ b/src/Mod/Draft/InitGui.py @@ -89,7 +89,7 @@ class DraftWorkbench (Workbench): "Draft_SelectGroup","Draft_SelectPlane", "Draft_ShowSnapBar","Draft_ToggleGrid","Draft_AutoGroup"] self.lineList = ["Draft_UndoLine","Draft_FinishLine","Draft_CloseLine"] - self.utils = ["Draft_VisGroup","Draft_Heal","Draft_FlipDimension", + self.utils = ["Draft_Layer","Draft_Heal","Draft_FlipDimension", "Draft_ToggleConstructionMode","Draft_ToggleContinueMode","Draft_Edit", "Draft_Slope","Draft_SetWorkingPlaneProxy","Draft_AddConstruction"] self.snapList = ['Draft_Snap_Lock','Draft_Snap_Midpoint','Draft_Snap_Perpendicular',