Exemple #1
0
def get_pattern(pat):
    """Get an SVG pattern."""
    patterns = utils.svg_patterns()

    if pat in patterns:
        return patterns[pat][0]
    return ''
Exemple #2
0
    def _set_properties(self, vobj):
        """Set the properties of objects if they don't exist."""
        if not hasattr(vobj, "Pattern"):
            _tip = "Defines a hatch pattern."
            vobj.addProperty("App::PropertyEnumeration", "Pattern", "Draft",
                             QT_TRANSLATE_NOOP("App::Property", _tip))
            vobj.Pattern = ["None"] + list(utils.svg_patterns().keys())

        if not hasattr(vobj, "PatternSize"):
            _tip = "Defines the size of the hatch pattern."
            vobj.addProperty("App::PropertyFloat", "PatternSize", "Draft",
                             QT_TRANSLATE_NOOP("App::Property", _tip))
            vobj.PatternSize = utils.get_param("HatchPatternSize", 1)
Exemple #3
0
    def __init__(self, vobj):
        self.Object = vobj.Object
        self.texture = None
        self.texcoords = None

        vobj.addProperty("App::PropertyEnumeration", "Pattern", "Draft",
                         QT_TRANSLATE_NOOP("App::Property",
                                           "Defines a hatch pattern"))
        vobj.addProperty("App::PropertyFloat", "PatternSize", "Draft",
                         QT_TRANSLATE_NOOP("App::Property",
                                           "Sets the size of the pattern"))
        vobj.Pattern = ["None"] + list(utils.svg_patterns().keys())
        vobj.PatternSize = 1

        # This class is assigned to the Proxy attribute
        vobj.Proxy = self
Exemple #4
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