From 65f484d7f1031ae7064ecceca462040c6717cb0f Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Tue, 13 Mar 2012 14:18:52 -0300 Subject: [PATCH] Added RFE 583 - remove coin3d image loading from Draft --- src/Mod/Draft/Draft.py | 86 +++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 22 deletions(-) diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index 7be81b5d5..c6fd4713c 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -278,6 +278,47 @@ def select(objs=None): for obj in objs: FreeCADGui.Selection.addSelection(obj) +def loadTexture(filename): + "loadTexture(filename): returns a SoSFImage from a file" + from pivy import coin + from PyQt4 import QtGui + try: + p = QtGui.QImage(filename) + size = coin.SbVec2s(p.width(), p.height()) + buffersize = p.numBytes() + numcomponents = int (buffersize / ( size[0] * size[1] )) + + img = coin.SoSFImage() + width = size[0] + height = size[1] + bytes = "" + + for y in range(height): + #line = width*numcomponents*(height-(y)); + for x in range(width): + rgb = p.pixel(x,y) + if numcomponents == 1: + bytes = bytes + chr(QtGui.qGray( rgb )) + elif numcomponents == 2: + bytes = bytes + chr(QtGui.qGray( rgb )) + bytes = bytes + chr(QtGui.qAlpha( rgb )) + elif numcomponents == 3: + bytes = bytes + chr(QtGui.qRed( rgb )) + bytes = bytes + chr(QtGui.qGreen( rgb )) + bytes = bytes + chr(QtGui.qBlue( rgb )) + elif numcomponents == 4: + bytes = bytes + chr(QtGui.qRed( rgb )) + bytes = bytes + chr(QtGui.qGreen( rgb )) + bytes = bytes + chr(QtGui.qBlue( rgb )) + bytes = bytes + chr(QtGui.qAlpha( rgb )) + #line += numcomponents + + img.setValue(size, numcomponents, bytes) + except: + return None + else: + return img + def makeCircle(radius, placement=None, face=True, startangle=None, endangle=None, support=None): '''makeCircle(radius,[placement,face,startangle,endangle]) or makeCircle(edge,[face]): @@ -1375,21 +1416,22 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"): # TODO add Radius constraits ok = True elif tp == "Rectangle": - for edge in obj.Shape.Edges: - nobj.addGeometry(edge.Curve) - if autoconstraints: - last = nobj.GeometryCount - 1 - segs = [last-3,last-2,last-1,last] - if obj.Placement.Rotation.Q == (0,0,0,1): - nobj.addConstraint(Constraint("Coincident",last-3,EndPoint,last-2,StartPoint)) - nobj.addConstraint(Constraint("Coincident",last-2,EndPoint,last-1,StartPoint)) - nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,last,StartPoint)) - nobj.addConstraint(Constraint("Coincident",last,EndPoint,last-3,StartPoint)) - nobj.addConstraint(Constraint("Horizontal",last-3)) - nobj.addConstraint(Constraint("Vertical",last-2)) - nobj.addConstraint(Constraint("Horizontal",last-1)) - nobj.addConstraint(Constraint("Vertical",last)) - ok = True + if obj.FilletRadius == 0: + for edge in obj.Shape.Edges: + nobj.addGeometry(edge.Curve) + if autoconstraints: + last = nobj.GeometryCount - 1 + segs = [last-3,last-2,last-1,last] + if obj.Placement.Rotation.Q == (0,0,0,1): + nobj.addConstraint(Constraint("Coincident",last-3,EndPoint,last-2,StartPoint)) + nobj.addConstraint(Constraint("Coincident",last-2,EndPoint,last-1,StartPoint)) + nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,last,StartPoint)) + nobj.addConstraint(Constraint("Coincident",last,EndPoint,last-3,StartPoint)) + nobj.addConstraint(Constraint("Horizontal",last-3)) + nobj.addConstraint(Constraint("Vertical",last-2)) + nobj.addConstraint(Constraint("Horizontal",last-1)) + nobj.addConstraint(Constraint("Vertical",last)) + ok = True elif tp in ["Wire","Polygon"]: closed = False if tp == "Polygon": @@ -1410,13 +1452,13 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"): if closed: nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,segs[0],StartPoint)) ok = True - elif obj.isDerivedFrom("Part::Feature"): + if (not ok) and obj.isDerivedFrom("Part::Feature"): if fcgeo.hasOnlyWires(obj.Shape): for w in obj.Shape.Wires: for edge in fcgeo.sortEdges(w.Edges): g = fcgeo.geom(edge) if g: - nobj.addGeometry(g) + nobj.addGeometry(g) if autoconstraints: last = nobj.GeometryCount segs = range(last-len(w.Edges),last-1) @@ -1543,8 +1585,6 @@ class _ViewProviderDraft: def __init__(self, obj): obj.Proxy = self - obj.addProperty("App::PropertyEnumeration","DrawStyle","Base", - "The line style of this object") self.Object = obj.Object def attach(self, obj): @@ -2148,9 +2188,11 @@ class _ViewProviderRectangle(_ViewProviderDraft): if prop == "TextureImage": r = vp.RootNode if os.path.exists(vp.TextureImage): - self.texture = coin.SoTexture2() - self.texture.filename = str(vp.TextureImage) - r.insertChild(self.texture,1) + im = loadTexture(vp.TextureImage) + if im: + self.texture = coin.SoTexture2() + self.texture.image = im + r.insertChild(self.texture,1) else: if self.texture: r.removeChild(self.texture)