Example #1
0
def make_dimension(p1, p2, p3=None, p4=None):
    """Create one of three types of dimension objects.

    In all dimensions the p3 parameter defines a point through which
    the dimension line will go through.

    The current line width and color will be used.

    Linear dimension
    ----------------
    - (p1, p2, p3): a simple linear dimension from p1 to p2

    - (object, i1, i2, p3): creates a linked dimension to the provided
      object (edge), measuring the distance between its vertices
      indexed i1 and i2

    Circular dimension
    ------------------
    - (arc, i1, mode, p3): creates a linked dimension to the given arc
      object, i1 is the index of the arc edge that will be measured;
      mode is either "radius" or "diameter".
    """
    if not App.ActiveDocument:
        _err("No active document. Aborting")
        return None

    new_obj = App.ActiveDocument.addObject("App::FeaturePython", "Dimension")
    LinearDimension(new_obj)

    if App.GuiUp:
        ViewProviderLinearDimension(new_obj.ViewObject)

    if isinstance(p1, App.Vector) and isinstance(p2, App.Vector):
        # Measure a straight distance between p1 and p2
        new_obj.Start = p1
        new_obj.End = p2
        if not p3:
            p3 = p2.sub(p1)
            p3.multiply(0.5)
            p3 = p1.add(p3)

    elif isinstance(p2, int) and isinstance(p3, int):
        # p1 is an object, and measure the distance between vertices p2 and p3
        # of this object
        linked = []
        idx = (p2, p3)
        linked.append((p1, "Vertex" + str(p2 + 1)))
        linked.append((p1, "Vertex" + str(p3 + 1)))
        new_obj.LinkedGeometry = linked
        new_obj.Support = p1

        # p4, and now p3, is the point through which the dimension line
        # will go through
        p3 = p4
        if not p3:
            # When used from the GUI command, this will never run
            # because p4 will always be assigned to a vector,
            # so p3 will never be `None`.
            # Moreover, `new_obj.Base` doesn't exist, and certainly `Shape`
            # doesn't exist, so if this ever runs it will be an error.
            v1 = new_obj.Base.Shape.Vertexes[idx[0]].Point
            v2 = new_obj.Base.Shape.Vertexes[idx[1]].Point
            p3 = v2.sub(v1)
            p3.multiply(0.5)
            p3 = v1.add(p3)

    elif isinstance(p3, str):
        # If the original p3 is a string, we are measuring a circular arc
        # p2 should be an integer number starting from 0
        linked = []
        linked.append((p1, "Edge" + str(p2 + 1)))

        if p3 == "radius":
            # linked.append((p1, "Center"))
            if App.GuiUp:
                new_obj.ViewObject.Override = "R $dim"
            new_obj.Diameter = False
        elif p3 == "diameter":
            # linked.append((p1, "Diameter"))
            if App.GuiUp:
                new_obj.ViewObject.Override = "Ø $dim"
            new_obj.Diameter = True
        new_obj.LinkedGeometry = linked
        new_obj.Support = p1

        # p4, and now p3, is the point through which the dimension line
        # will go through
        p3 = p4
        if not p3:
            p3 = p1.Shape.Edges[p2].Curve.Center.add(App.Vector(1, 0, 0))

    # This p3 is the point through which the dimension line will pass,
    # but this may not be the original p3, it could have been p4
    # depending on the first three parameter values
    new_obj.Dimline = p3

    if hasattr(App, "DraftWorkingPlane"):
        normal = App.DraftWorkingPlane.axis
    else:
        normal = App.Vector(0, 0, 1)

    if App.GuiUp:
        # invert the normal if we are viewing it from the back
        vnorm = gui_utils.get3DView().getViewDirection()

        if vnorm.getAngle(normal) < math.pi / 2:
            normal = normal.negative()

    new_obj.Normal = normal

    if App.GuiUp:
        gui_utils.format_object(new_obj)
        gui_utils.select(new_obj)

    return new_obj
Example #2
0
def move(objectslist, vector, copy=False):
    """move(objects,vector,[copy])
    
    Move the objects contained in objects (that can be an object or a
    list of objects) in the direction and distance indicated by the given
    vector. 

    Parameters
    ----------
    objectslist : list

    vector : Base.Vector
        Delta Vector to move the clone from the original position. 

    copy : bool
        If copy is True, the actual objects are not moved, but copies
        are created instead. 

    Return
    ----------
    The objects (or their copies) are returned.
    """
    utils.type_check([(vector, App.Vector), (copy,bool)], "move")
    if not isinstance(objectslist, list): objectslist = [objectslist]
    objectslist.extend(utils.get_movable_children(objectslist))
    newobjlist = []
    newgroups = {}
    objectslist = utils.filter_objects_for_modifiers(objectslist, copy)
    for obj in objectslist:
        newobj = None
        # real_vector have been introduced to take into account
        # the possibility that object is inside an App::Part
        if hasattr(obj, "getGlobalPlacement"):
            v_minus_global = obj.getGlobalPlacement().inverse().Rotation.multVec(vector)
            real_vector = obj.Placement.Rotation.multVec(v_minus_global)
        else:
            real_vector = vector
        if utils.get_type(obj) == "Point":
            v = App.Vector(obj.X,obj.Y,obj.Z)
            v = v.add(real_vector)
            if copy:
                newobj = make_copy(obj)
            else:
                newobj = obj
            newobj.X = v.x
            newobj.Y = v.y
            newobj.Z = v.z
        elif obj.isDerivedFrom("App::DocumentObjectGroup"):
            pass
        elif hasattr(obj,'Shape'):
            if copy:
                newobj = make_copy(obj)
            else:
                newobj = obj
            pla = newobj.Placement
            pla.move(real_vector)
        elif utils.get_type(obj) == "Annotation":
            if copy:
                newobj = App.ActiveDocument.addObject("App::Annotation",
                                                      utils.getRealName(obj.Name))
                newobj.LabelText = obj.LabelText
                if App.GuiUp:
                    gui_utils.formatObject(newobj,obj)
            else:
                newobj = obj
            newobj.Position = obj.Position.add(real_vector)
        elif utils.get_type(obj) == "Text":
            if copy:
                # TODO: Why make_copy do not handle Text object??
                newobj = App.ActiveDocument.addObject("App::FeaturePython",
                                                      utils.getRealName(obj.Name))
                Text(newobj)
                if App.GuiUp:
                    ViewProviderText(newobj.ViewObject)
                    gui_utils.formatObject(newobj,obj)
                newobj.Text = obj.Text
                newobj.Placement = obj.Placement
                if App.GuiUp:
                    gui_utils.formatObject(newobj,obj)
            else:
                newobj = obj
            newobj.Placement.Base = obj.Placement.Base.add(real_vector)
        elif utils.get_type(obj) in ["Dimension","LinearDimension"]:
            if copy:
                # TODO: Why make_copy do not handle Dimension object??
                # TODO: Support also Label and Angular dimension
                newobj = App.ActiveDocument.addObject("App::FeaturePython",
                                                      utils.getRealName(obj.Name))
                LinearDimension(newobj)
                if App.GuiUp:
                    ViewProviderLinearDimension(newobj.ViewObject)
                    gui_utils.formatObject(newobj,obj)
            else:
                newobj = obj
            newobj.Start = obj.Start.add(real_vector)
            newobj.End = obj.End.add(real_vector)
            newobj.Dimline = obj.Dimline.add(real_vector)
        else:
            if copy and obj.isDerivedFrom("Mesh::Feature"):
                print("Mesh copy not supported at the moment") # TODO
            newobj = obj
            if "Placement" in obj.PropertiesList:
                pla = obj.Placement
                pla.move(real_vector)
        if newobj is not None:
            newobjlist.append(newobj)
        if copy:
            for p in obj.InList:
                if p.isDerivedFrom("App::DocumentObjectGroup") and (p in objectslist):
                    g = newgroups.setdefault(p.Name,App.ActiveDocument.addObject(p.TypeId,p.Name))
                    g.addObject(newobj)
                    break
                if utils.get_type(p) == "Layer":
                    p.Proxy.addObject(p,newobj)
    if copy and utils.get_param("selectBaseObjects",False):
        gui_utils.select(objectslist)
    else:
        gui_utils.select(newobjlist)
    if len(newobjlist) == 1: return newobjlist[0]
    return newobjlist
Example #3
0
def make_dimension(p1, p2, p3=None, p4=None):
    """makeDimension(p1,p2,[p3]) or makeDimension(object,i1,i2,p3)
    or makeDimension(objlist,indices,p3): Creates a Dimension object with
    the dimension line passign through p3.The current line width and color
    will be used. There are multiple  ways to create a dimension, depending on
    the arguments you pass to it:
    - (p1,p2,p3): creates a standard dimension from p1 to p2
    - (object,i1,i2,p3): creates a linked dimension to the given object,
    measuring the distance between its vertices indexed i1 and i2
    - (object,i1,mode,p3): creates a linked dimension
    to the given object, i1 is the index of the (curved) edge to measure,
    and mode is either "radius" or "diameter".
    """
    if not App.ActiveDocument:
        App.Console.PrintError("No active document. Aborting\n")
        return
    obj = App.ActiveDocument.addObject("App::FeaturePython", "Dimension")
    LinearDimension(obj)
    if App.GuiUp:
        ViewProviderLinearDimension(obj.ViewObject)
    if isinstance(p1, App.Vector) and isinstance(p2, App.Vector):
        obj.Start = p1
        obj.End = p2
        if not p3:
            p3 = p2.sub(p1)
            p3.multiply(0.5)
            p3 = p1.add(p3)
    elif isinstance(p2, int) and isinstance(p3, int):
        l = []
        idx = (p2, p3)
        l.append((p1, "Vertex" + str(p2 + 1)))
        l.append((p1, "Vertex" + str(p3 + 1)))
        obj.LinkedGeometry = l
        obj.Support = p1
        p3 = p4
        if not p3:
            v1 = obj.Base.Shape.Vertexes[idx[0]].Point
            v2 = obj.Base.Shape.Vertexes[idx[1]].Point
            p3 = v2.sub(v1)
            p3.multiply(0.5)
            p3 = v1.add(p3)
    elif isinstance(p3, str):
        l = []
        l.append((p1, "Edge" + str(p2 + 1)))
        if p3 == "radius":
            #l.append((p1,"Center"))
            if App.GuiUp:
                obj.ViewObject.Override = "R $dim"
            obj.Diameter = False
        elif p3 == "diameter":
            #l.append((p1,"Diameter"))
            if App.GuiUp:
                obj.ViewObject.Override = "Ø $dim"
            obj.Diameter = True
        obj.LinkedGeometry = l
        obj.Support = p1
        p3 = p4
        if not p3:
            p3 = p1.Shape.Edges[p2].Curve.Center.add(App.Vector(1, 0, 0))
    obj.Dimline = p3
    if hasattr(App, "DraftWorkingPlane"):
        normal = App.DraftWorkingPlane.axis
    else:
        normal = App.Vector(0, 0, 1)
    if App.GuiUp:
        # invert the normal if we are viewing it from the back
        vnorm = gui_utils.get3DView().getViewDirection()
        if vnorm.getAngle(normal) < math.pi / 2:
            normal = normal.negative()
    obj.Normal = normal

    if App.GuiUp:
        gui_utils.format_object(obj)
        gui_utils.select(obj)

    return obj
Example #4
0
def make_copy(obj, force=None, reparent=False):
    """makeCopy(object, [force], [reparent])
    
    Make an exact copy of an object and return it.
    
    Parameters
    ----------
    obj :
        Object to copy.

    force :
        TODO: Describe.

    reparent :
        TODO: Describe.
    """
    if not App.ActiveDocument:
        App.Console.PrintError("No active document. Aborting\n")
        return

    if (utils.get_type(obj) == "Rectangle") or (force == "Rectangle"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Rectangle(newobj)
        if App.GuiUp:
            ViewProviderRectangle(newobj.ViewObject)

    elif (utils.get_type(obj) == "Point") or (force == "Point"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Point(newobj)
        if App.GuiUp:
            ViewProviderPoint(newobj.ViewObject)

    elif (utils.get_type(obj) in ["Dimension", "LinearDimension"
                                  ]) or (force == "Dimension"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        LinearDimension(newobj)
        if App.GuiUp:
            ViewProviderLinearDimension(newobj.ViewObject)

    elif (utils.get_type(obj) == "Wire") or (force == "Wire"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Wire(newobj)
        if App.GuiUp:
            ViewProviderWire(newobj.ViewObject)

    elif (utils.get_type(obj) == "Circle") or (force == "Circle"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Circle(newobj)
        if App.GuiUp:
            ViewProviderDraft(newobj.ViewObject)

    elif (utils.get_type(obj) == "Polygon") or (force == "Polygon"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Polygon(newobj)
        if App.GuiUp:
            ViewProviderDraft(newobj.ViewObject)

    elif (utils.get_type(obj) == "BSpline") or (force == "BSpline"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        BSpline(newobj)
        if App.GuiUp:
            ViewProviderWire(newobj.ViewObject)

    elif (utils.get_type(obj) == "Block") or (force == "BSpline"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        Block(newobj)
        if App.GuiUp:
            ViewProviderDraftPart(newobj.ViewObject)

    # drawingview became obsolete with v 0.19
    # TODO: uncomment after splitting DrawingView object from draft py

    #elif (utils.get_type(obj) == "DrawingView") or (force == "DrawingView"):
    #_name = utils.get_real_name(obj.Name)
    #newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
    #DrawingView(newobj)

    elif (utils.get_type(obj) == "Structure") or (force == "Structure"):
        import ArchStructure
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        ArchStructure._Structure(newobj)
        if App.GuiUp:
            ArchStructure._ViewProviderStructure(newobj.ViewObject)

    elif (utils.get_type(obj) == "Wall") or (force == "Wall"):
        import ArchWall
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        ArchWall._Wall(newobj)
        if App.GuiUp:
            ArchWall._ViewProviderWall(newobj.ViewObject)

    elif (utils.get_type(obj) == "Window") or (force == "Window"):
        import ArchWindow
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        ArchWindow._Window(newobj)
        if App.GuiUp:
            ArchWindow._ViewProviderWindow(newobj.ViewObject)

    elif (utils.get_type(obj) == "Panel") or (force == "Panel"):
        import ArchPanel
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject(obj.TypeId, _name)
        ArchPanel._Panel(newobj)
        if App.GuiUp:
            ArchPanel._ViewProviderPanel(newobj.ViewObject)

    elif (utils.get_type(obj) == "Sketch") or (force == "Sketch"):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject("Sketcher::SketchObject", _name)
        for geo in obj.Geometry:
            newobj.addGeometry(geo)
        for con in obj.Constraints:
            newobj.addConstraint(con)

    elif hasattr(obj, 'Shape'):
        _name = utils.get_real_name(obj.Name)
        newobj = App.ActiveDocument.addObject("Part::Feature", _name)
        newobj.Shape = obj.Shape

    else:
        print("Error: Object type cannot be copied")
        return None

    for p in obj.PropertiesList:
        if not p in ["Proxy", "ExpressionEngine"]:
            if p in newobj.PropertiesList:
                if not "ReadOnly" in newobj.getEditorMode(p):
                    try:
                        setattr(newobj, p, obj.getPropertyByName(p))
                    except AttributeError:
                        try:
                            setattr(newobj, p, obj.getPropertyByName(p).Value)
                        except AttributeError:
                            pass

    if reparent:
        parents = obj.InList
        if parents:
            for par in parents:
                if par.isDerivedFrom("App::DocumentObjectGroup"):
                    par.addObject(newobj)
                else:
                    for prop in par.PropertiesList:
                        if getattr(par, prop) == obj:
                            setattr(par, prop, newobj)

    gui_utils.format_object(newobj, obj)
    return newobj