Exemplo n.º 1
0
 def attach(self,vobj):
     self.Object = vobj.Object
     from pivy import coin
     tex = coin.SoTexture2()
     tex.image = Draft.loadTexture(Draft.svgpatterns()['simple'][1], 128)
     texcoords = coin.SoTextureCoordinatePlane()
     s = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch").GetFloat("patternScale",0.01)
     texcoords.directionS.setValue(s,0,0)
     texcoords.directionT.setValue(0,s,0)
     self.fcoords = coin.SoCoordinate3()
     self.fset = coin.SoIndexedFaceSet()
     sep = coin.SoSeparator()
     sep.addChild(tex)
     sep.addChild(texcoords)
     sep.addChild(self.fcoords)
     sep.addChild(self.fset)
     vobj.RootNode.addChild(sep)
     ArchComponent.ViewProviderComponent.attach(self,vobj)
Exemplo n.º 2
0
    def onChanged(self, vobj, prop):
        """Run when a view property is changed.

        Override this method to handle the behavior
        of the view provider depending on changes that occur to its properties
        such as line color, line width, point color, point size,
        draw style, shape color, transparency, and others.

        This method  updates the texture and pattern if
        the properties `TextureImage`, `Pattern`, `DiffuseColor`,
        and `PatternSize` change.

        Parameters
        ----------
        vobj : the view provider of the scripted object.
            This is `obj.ViewObject`.

        prop : str
            Name of the property that was modified.
        """
        # treatment of patterns and image textures
        if prop in ("TextureImage", "Pattern", "DiffuseColor"):
            if hasattr(self.Object, "Shape"):
                if self.Object.Shape.Faces:
                    path = None
                    if hasattr(vobj, "TextureImage"):
                        if vobj.TextureImage:
                            path = vobj.TextureImage
                    if not path:
                        if hasattr(vobj, "Pattern"):
                            if str(vobj.Pattern) in list(utils.svg_patterns().keys()):
                                path = utils.svg_patterns()[vobj.Pattern][1]
                            else:
                                path = "None"
                    if path and vobj.RootNode:
                        if vobj.RootNode.getChildren().getLength() > 2:
                            if vobj.RootNode.getChild(2).getChildren().getLength() > 0:
                                if vobj.RootNode.getChild(2).getChild(0).getChildren().getLength() > 2:
                                    r = vobj.RootNode.getChild(2).getChild(0).getChild(2)
                                    i = QtCore.QFileInfo(path)
                                    if self.texture:
                                        r.removeChild(self.texture)
                                        self.texture = None
                                    if self.texcoords:
                                        r.removeChild(self.texcoords)
                                        self.texcoords = None
                                    if i.exists():
                                        size = None
                                        if ".SVG" in path.upper():
                                            size = utils.get_param("HatchPatternResolution", 128)
                                            if not size:
                                                size = 128
                                        im = gui_utils.load_texture(path, size)
                                        if im:
                                            self.texture = coin.SoTexture2()
                                            self.texture.image = im
                                            r.insertChild(self.texture, 1)
                                            if size:
                                                s = 1
                                                if hasattr(vobj, "PatternSize"):
                                                    if vobj.PatternSize:
                                                        s = vobj.PatternSize
                                                self.texcoords = coin.SoTextureCoordinatePlane()
                                                self.texcoords.directionS.setValue(s, 0, 0)
                                                self.texcoords.directionT.setValue(0, s, 0)
                                                r.insertChild(self.texcoords, 2)
        elif prop == "PatternSize":
            if hasattr(self, "texcoords"):
                if self.texcoords:
                    s = 1
                    if vobj.PatternSize:
                        s = vobj.PatternSize
                    vS = App.Vector(self.texcoords.directionS.getValue().getValue())
                    vT = App.Vector(self.texcoords.directionT.getValue().getValue())
                    vS.Length = s
                    vT.Length = s
                    self.texcoords.directionS.setValue(vS.x, vS.y, vS.z)
                    self.texcoords.directionT.setValue(vT.x, vT.y, vT.z)
        return