Пример #1
0
    def Activated(self):
        r"""The Add Anchor Command was activated"""
        # selection = Gui.Selection.getSelection()
        selection_ex = Gui.Selection.getSelectionEx()

        debug("len selection_ex = %i" % len(selection_ex))

        if len(selection_ex) != 1:
            msg = "Anchors : " \
                  "Select feature(s) on only 1 solid to add anchors to"
            error(msg)
            return
        else:
            # https://forum.freecadweb.org/viewtopic.php?t=7249
            unique_selection = selection_ex[0]
            selected_object = unique_selection.Object
            debug("  Selection : %s || %s" %
                  (selected_object, selected_object.Shape.ShapeType))

            subselected_objects = unique_selection.SubObjects

            for i, subselected_object in enumerate(subselected_objects):
                debug("SubSelection : %s || %s" %
                      (subselected_object, type(subselected_object)))

                p, u, v = puv(subselected_object)

                # make_anchor_feature(p, u, v)

                print("SubElementName : %s" %
                      unique_selection.SubElementNames[0])

                a = App.ActiveDocument.addObject("App::FeaturePython",
                                                 "Anchor")
                Anchor(a,
                       p,
                       u,
                       v,
                       topo_element=(unique_selection.Object,
                                     unique_selection.SubElementNames[i]))
                ViewProviderAnchor(a.ViewObject)

                # -- Add the anchor to the App::PropertyLinkList
                #    of the selected AnchorableObject --
                try:
                    l = selected_object.Anchors
                    l.append(a)
                    selected_object.Anchors = l
                except AttributeError:
                    msg = "Are you adding anchors to an AnchorableObject?"
                    error(msg)
    def Activated(self):
        r"""The Save anchorable object Command was activated"""
        # selection = Gui.Selection.getSelection()
        selection_ex = Gui.Selection.getSelectionEx()

        debug("len selection_ex = %i" % len(selection_ex))

        if len(selection_ex) != 1:
            msg = "Anchors : " \
                  "Select only 1 anchorable object to save"
            error(msg)
            return
        else:
            # https://forum.freecadweb.org/viewtopic.php?t=7249
            unique_selection = selection_ex[0]
            selected_object = unique_selection.Object
            debug("  Selection : %s || %s" % (selected_object,
                                              selected_object.Shape.ShapeType))

            #  check is anchorable object
            if is_anchorable_object(selected_object):
                dialog = QtGui.QFileDialog.getSaveFileName(
                    filter="Stepzip files (*.stepzip)")
                #  save shape as step
                stepzip_path = dialog[0]
                stepfile = splitext(stepzip_path)[0] + ".stp"
                anchorsfile = splitext(stepzip_path)[0] + ".anchors"
                selected_object.Shape.exportStep(stepfile)
                #  save anchors file (+ feature attachment)

                # TODO : save more info about anchor (sub element link)

                anchors_descs = []
                for anchor in selected_object.Anchors:
                    anchors_descs.append("%s %f,%f,%f,%f,%f,%f,%f,%f,%f" %
                                        (anchor.Label,
                                         anchor.p[0], anchor.p[1], anchor.p[2],
                                         anchor.u[0], anchor.u[1], anchor.u[2],
                                         anchor.v[0], anchor.v[1], anchor.v[2]))
                with open(anchorsfile, 'w') as f:
                    f.write("\n".join(anchors_descs))

                #  zip it to a stepzip
                create_stepzip(stepfile, anchorsfile)
                info("Anchorable object saved to %s" % dialog[0])
            else:
                error("The object to save should be an anchorable object")
Пример #3
0
# coding: utf-8
r"""Anchor Add Command"""

from os.path import join, dirname

import FreeCAD as App

from freecad_logging import debug, error
from anchor import Anchor, ViewProviderAnchor
from puv import puv

if App.GuiUp:
    import FreeCADGui as Gui
else:
    msg_no_ui = "Adding an anchorable object requires the FreeCAD Gui to be up"
    error(msg_no_ui)


class CommandAnchorAdd:
    r"""AnchorAddCommand
    
    Command to add an anchor to a Part
    
    """
    def __init__(self):
        pass

    def Activated(self):
        r"""The Add Anchor Command was activated"""
        # selection = Gui.Selection.getSelection()
        selection_ex = Gui.Selection.getSelectionEx()
# You should have received a copy of the GNU General Public License
# along with cadracks-freecad-workbench.  If not, see <https://www.gnu.org/licenses/>.
r"""Anchor Add Command"""

from os.path import join, dirname

import FreeCAD as App

from freecad_logging import info, error, debug
from anchorable_object import make_anchorable_object_feature

if App.GuiUp:
    import FreeCADGui as Gui
else:
    msg = "Adding an anchorable object requires the FreeCAD Gui to be up"
    error(msg)


class CommandAnchorableObjectAdd:
    r"""Command to make an object anchorable
    
    An anchorable object is a container for:
    - a FreeCAD object
    - one or more Anchors

    """
    def __init__(self):
        pass

    def Activated(self):
        r"""The command has been clicked"""