Skip to content

Commit

Permalink
Draft: split clone from Draft.py
Browse files Browse the repository at this point in the history
.


.
  • Loading branch information
carlopav authored and yorikvanhavre committed Apr 29, 2020
1 parent f1eaa0b commit e4bfb0b
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 210 deletions.
3 changes: 3 additions & 0 deletions src/Mod/Draft/CMakeLists.txt
Expand Up @@ -67,6 +67,7 @@ SET(Draft_make_functions
draftmake/make_bezcurve.py
draftmake/make_bspline.py
draftmake/make_circle.py
draftmake/make_clone.py
draftmake/make_line.py
draftmake/make_polygon.py
draftmake/make_rectangle.py
Expand All @@ -80,6 +81,7 @@ SET(Draft_objects
draftobjects/bspline.py
draftobjects/circulararray.py
draftobjects/circle.py
draftobjects/clone.py
draftobjects/orthoarray.py
draftobjects/polararray.py
draftobjects/arc_3points.py
Expand All @@ -97,6 +99,7 @@ SET(Draft_view_providers
draftviewproviders/__init__.py
draftviewproviders/view_base.py
draftviewproviders/view_circulararray.py
draftviewproviders/view_clone.py
draftviewproviders/view_orthoarray.py
draftviewproviders/view_polararray.py
draftviewproviders/view_draft_annotation.py
Expand Down
217 changes: 7 additions & 210 deletions src/Mod/Draft/Draft.py
Expand Up @@ -228,6 +228,13 @@
# for compatibility with older versions
_ViewProviderBezCurve = ViewProviderWire

# clone
from draftmake.make_clone import make_clone, clone
from draftobjects.clone import Clone, _Clone
if FreeCAD.GuiUp:
from draftviewproviders.view_clone import ViewProviderClone
from draftviewproviders.view_clone import _ViewProviderClone

#---------------------------------------------------------------------------
# Draft annotation objects
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -1923,90 +1930,6 @@ def makeShapeString(String,FontFile,Size = 100,Tracking = 0):
obj.recompute()
return obj

def clone(obj,delta=None,forcedraft=False):
"""clone(obj,[delta,forcedraft]): makes a clone of the given object(s). The clone is an exact,
linked copy of the given object. If the original object changes, the final object
changes too. Optionally, you can give a delta Vector to move the clone from the
original position. If forcedraft is True, the resulting object is a Draft clone
even if the input object is an Arch object."""

prefix = getParam("ClonePrefix","")
cl = None
if prefix:
prefix = prefix.strip()+" "
if not isinstance(obj,list):
obj = [obj]
if (len(obj) == 1) and obj[0].isDerivedFrom("Part::Part2DObject"):
cl = FreeCAD.ActiveDocument.addObject("Part::Part2DObjectPython","Clone2D")
cl.Label = prefix + obj[0].Label + " (2D)"
elif (len(obj) == 1) and (hasattr(obj[0],"CloneOf") or (getType(obj[0]) == "BuildingPart")) and (not forcedraft):
# arch objects can be clones
import Arch
if getType(obj[0]) == "BuildingPart":
cl = Arch.makeComponent()
else:
try:
clonefunc = getattr(Arch,"make"+obj[0].Proxy.Type)
except:
pass # not a standard Arch object... Fall back to Draft mode
else:
cl = clonefunc()
if cl:
base = getCloneBase(obj[0])
cl.Label = prefix + base.Label
cl.CloneOf = base
if hasattr(cl,"Material") and hasattr(obj[0],"Material"):
cl.Material = obj[0].Material
if getType(obj[0]) != "BuildingPart":
cl.Placement = obj[0].Placement
try:
cl.Role = base.Role
cl.Description = base.Description
cl.Tag = base.Tag
except:
pass
if gui:
formatObject(cl,base)
cl.ViewObject.DiffuseColor = base.ViewObject.DiffuseColor
if getType(obj[0]) in ["Window","BuildingPart"]:
from DraftGui import todo
todo.delay(Arch.recolorize,cl)
select(cl)
return cl
# fall back to Draft clone mode
if not cl:
cl = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Clone")
cl.addExtension("Part::AttachExtensionPython", None)
cl.Label = prefix + obj[0].Label
_Clone(cl)
if gui:
_ViewProviderClone(cl.ViewObject)
cl.Objects = obj
if delta:
cl.Placement.move(delta)
elif (len(obj) == 1) and hasattr(obj[0],"Placement"):
cl.Placement = obj[0].Placement
formatObject(cl,obj[0])
if hasattr(cl,"LongName") and hasattr(obj[0],"LongName"):
cl.LongName = obj[0].LongName
if gui and (len(obj) > 1):
cl.ViewObject.Proxy.resetColors(cl.ViewObject)
select(cl)
return cl

def getCloneBase(obj,strict=False):
"""getCloneBase(obj,[strict]): returns the object cloned by this object, if
any, or this object if it is no clone. If strict is True, if this object is
not a clone, this function returns False"""
if hasattr(obj,"CloneOf"):
if obj.CloneOf:
return getCloneBase(obj.CloneOf)
if getType(obj) == "Clone":
return obj.Objects[0]
if strict:
return False
return obj


def mirror(objlist, p1, p2):
"""mirror(objlist, p1, p2)
Expand Down Expand Up @@ -3758,132 +3681,6 @@ def onChanged(self, vobj, prop):
def getIcon(self):
return ":/icons/Draft_Dot.svg"

class _Clone(_DraftObject):
"""The Clone object"""

def __init__(self,obj):
_DraftObject.__init__(self,obj,"Clone")
obj.addProperty("App::PropertyLinkListGlobal","Objects","Draft",QT_TRANSLATE_NOOP("App::Property","The objects included in this clone"))
obj.addProperty("App::PropertyVector","Scale","Draft",QT_TRANSLATE_NOOP("App::Property","The scale factor of this clone"))
obj.addProperty("App::PropertyBool","Fuse","Draft",QT_TRANSLATE_NOOP("App::Property","If this clones several objects, this specifies if the result is a fusion or a compound"))
obj.Scale = Vector(1,1,1)

def join(self,obj,shapes):
fuse = getattr(obj,'Fuse',False)
if fuse:
tmps = []
for s in shapes:
tmps += s.Solids
if not tmps:
for s in shapes:
tmps += s.Faces
if not tmps:
for s in shapes:
tmps += s.Edges
shapes = tmps
if len(shapes) == 1:
return shapes[0]
import Part
if fuse:
try:
sh = shapes[0].multiFuse(shapes[1:])
sh = sh.removeSplitter()
except:
pass
else:
return sh
return Part.makeCompound(shapes)

def execute(self,obj):
import Part, DraftGeomUtils
pl = obj.Placement
shapes = []
if obj.isDerivedFrom("Part::Part2DObject"):
# if our clone is 2D, make sure all its linked geometry is 2D too
for o in obj.Objects:
if not o.getLinkedObject(True).isDerivedFrom("Part::Part2DObject"):
FreeCAD.Console.PrintWarning("Warning 2D Clone "+obj.Name+" contains 3D geometry")
return
for o in obj.Objects:
sh = Part.getShape(o)
if not sh.isNull():
shapes.append(sh)
if shapes:
sh = self.join(obj,shapes)
m = FreeCAD.Matrix()
if hasattr(obj,"Scale") and not sh.isNull():
sx,sy,sz = obj.Scale
if not DraftVecUtils.equals(obj.Scale,Vector(1,1,1)):
op = sh.Placement
sh.Placement = FreeCAD.Placement()
m.scale(obj.Scale)
if sx == sy == sz:
sh.transformShape(m)
else:
sh = sh.transformGeometry(m)
sh.Placement = op
obj.Shape = sh

obj.Placement = pl
if hasattr(obj,"positionBySupport"):
obj.positionBySupport()

def getSubVolume(self,obj,placement=None):
# this allows clones of arch windows to return a subvolume too
if obj.Objects:
if hasattr(obj.Objects[0],"Proxy"):
if hasattr(obj.Objects[0].Proxy,"getSubVolume"):
if not placement:
# clones must displace the original subvolume too
placement = obj.Placement
return obj.Objects[0].Proxy.getSubVolume(obj.Objects[0],placement)
return None

class _ViewProviderClone:
"""a view provider that displays a Clone icon instead of a Draft icon"""

def __init__(self,vobj):
vobj.Proxy = self

def getIcon(self):
return ":/icons/Draft_Clone.svg"

def __getstate__(self):
return None

def __setstate__(self, state):
return None

def getDisplayModes(self, vobj):
modes=[]
return modes

def setDisplayMode(self, mode):
return mode

def resetColors(self, vobj):
colors = []
for o in getGroupContents(vobj.Object.Objects):
if o.isDerivedFrom("Part::Feature"):
if len(o.ViewObject.DiffuseColor) > 1:
colors.extend(o.ViewObject.DiffuseColor)
else:
c = o.ViewObject.ShapeColor
c = (c[0],c[1],c[2],o.ViewObject.Transparency/100.0)
for f in o.Shape.Faces:
colors.append(c)
elif o.hasExtension("App::GeoFeatureGroupExtension"):
for so in vobj.Object.Group:
if so.isDerivedFrom("Part::Feature"):
if len(so.ViewObject.DiffuseColor) > 1:
colors.extend(so.ViewObject.DiffuseColor)
else:
c = so.ViewObject.ShapeColor
c = (c[0],c[1],c[2],so.ViewObject.Transparency/100.0)
for f in so.Shape.Faces:
colors.append(c)
if colors:
vobj.DiffuseColor = colors

class _ViewProviderDraftArray(_ViewProviderDraft):
"""a view provider that displays a Array icon instead of a Draft icon"""
Expand Down

0 comments on commit e4bfb0b

Please sign in to comment.