File size: 5,091 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# SPDX-License-Identifier: LGPL-2.1-or-later

# /***************************************************************************
#  *   Copyright (c) 2023 David Friedli <david[at]friedli-be.ch>             *
#  *                                                                         *
#  *   This file is part of FreeCAD.                                         *
#  *                                                                         *
#  *   FreeCAD is free software: you can redistribute it and/or modify it    *
#  *   under the terms of the GNU Lesser General Public License as           *
#  *   published by the Free Software Foundation, either version 2.1 of the  *
#  *   License, or (at your option) any later version.                       *
#  *                                                                         *
#  *   FreeCAD is distributed in the hope that it will be useful, but        *
#  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
#  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
#  *   Lesser General Public License for more details.                       *
#  *                                                                         *
#  *   You should have received a copy of the GNU Lesser General Public      *
#  *   License along with FreeCAD. If not, see                               *
#  *   <https://www.gnu.org/licenses/>.                                      *
#  *                                                                         *
#  **************************************************************************/

import FreeCAD
from FreeCAD import Units, Placement
from UtilsMeasure import MeasureBasePython
from PySide.QtCore import QT_TRANSLATE_NOOP


__title__ = "Measure Center of Mass Object"
__author__ = "David Friedli"
__url__ = "http://www.freecad.org"


"""
    The Measure cpp object defines a result and a placement property. The Python measure type
    adds it's own specific properties. Once the object is recomputed the parent properties are updated
    based on the specific python properties.

    We'll need some kind of interface for the measure command which exposes "parseSelection", "isValidSelection" etc.

"""


def makeMeasureCOM(name="CenterOfMass"):
    """makeMeasureCOM(name): make a CenterofMass measurement"""
    obj = FreeCAD.ActiveDocument.addObject("Measure::MeasurePython", name)
    MeasureCOM(obj)
    return obj


class MeasureCOM(MeasureBasePython):
    "The MeasureCOM object"

    def __init__(self, obj):
        obj.Proxy = self

        obj.addProperty(
            "App::PropertyLinkSubGlobal",
            "Element",
            "",
            QT_TRANSLATE_NOOP("App::Property", "Element to measure"),
            locked=True,
        )
        obj.addProperty(
            "App::PropertyPosition",
            "Result",
            "",
            QT_TRANSLATE_NOOP("App::PropertyVector", "The result location"),
            locked=True,
        )

    @classmethod
    def isValidSelection(cls, selection):
        if not len(selection) == 1:
            return False

        element = selection[0]
        ob = element["object"]
        subName = element["subName"]

        if not ob:
            return

        sub = ob.getSubObject(subName)
        if not sub:
            return

        if not hasattr(sub, "CenterOfMass"):
            return

        return True

    @classmethod
    def isPrioritySelection(cls, selection):
        return False

    @classmethod
    def getInputProps(cls):
        return ("Element",)

    def getSubject(self, obj):
        if not obj:
            return ()

        element = obj.Element
        if not element:
            return ()

        ob = element[0]
        if not ob:
            return ()
        return (ob,)

    def parseSelection(self, obj, selection):
        item = selection[0]
        o = item["object"]
        obj.Element = (o, item["subName"])

    def getResultString(self, obj):
        values = [Units.Quantity(v, Units.Length).getUserPreferred()[0] for v in obj.Result]
        return "COM\nX: {}\nY: {}\nZ: {}".format(*values)

    def execute(self, obj):
        element = obj.Element
        if not element:
            return

        ob = element[0]
        subElements = element[1]

        if subElements:
            subName = subElements[0]
            sub = ob.getSubObject(subName)

            if not sub or not hasattr(sub, "CenterOfMass"):
                return
            com = sub.CenterOfMass

        else:
            # Get Center of Mass of the object
            if not hasattr(ob, "Shape"):
                return

            shape = ob.Shape
            if not hasattr(shape, "CenterOfMass"):
                return

            com = shape.CenterOfMass

        obj.Result = com
        placement = Placement()
        placement.Base = com
        obj.Placement = placement

    def onChanged(self, obj, prop):
        """Do something when a property has changed"""

        if prop == "Element":
            self.execute(obj)