File size: 7,238 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#! python
# SPDX-License-Identifier: LGPL-2.1-or-later

# ShapeContent.py
# 2021, by Mark Ganson <TheMarkster>
# LGPL 2.1 or later

# this file is called from c++ in TaskCheckGeometry.cpp
# from buildShapeContent()

import FreeCAD as App
import Part

translate = App.Qt.translate


def roundVector(v, dec):
    return str([round(v[0], dec), round(v[1], dec), round(v[2], dec)])


def buildShapeContent(objArg, decimals=2, advancedShapeContent=True):
    linkName = ""
    if objArg.isDerivedFrom("App::Link"):
        linkName = "<" + objArg.Name + "> "

    obj = objArg
    shp = Part.getShape(objArg)
    typeStr = str(shp.ShapeType)
    lbl = "" if obj.Name == obj.Label else " (" + obj.Label + ")"
    result = linkName + obj.Name + lbl + "\n"
    result += translate("TaskCheckGeometryResults", "Shape type") + ":  " + typeStr + "\n"
    result += (
        translate("TaskCheckGeometryResults", "Vertices") + ":  " + str(len(shp.Vertexes)) + "\n"
    )
    result += translate("TaskCheckGeometryResults", "Edges") + ":  " + str(len(shp.Edges)) + "\n"
    result += translate("TaskCheckGeometryResults", "Wires") + ":  " + str(len(shp.Wires)) + "\n"
    result += translate("TaskCheckGeometryResults", "Faces") + ":  " + str(len(shp.Faces)) + "\n"
    result += translate("TaskCheckGeometryResults", "Shells") + ":  " + str(len(shp.Shells)) + "\n"
    result += translate("TaskCheckGeometryResults", "Solids") + ":  " + str(len(shp.Solids)) + "\n"
    result += (
        translate("TaskCheckGeometryResults", "CompSolids")
        + ":  "
        + str(len(shp.CompSolids))
        + "\n"
    )
    result += (
        translate("TaskCheckGeometryResults", "Compounds") + ":  " + str(len(shp.Compounds)) + "\n"
    )
    result += (
        translate("TaskCheckGeometryResults", "Shapes")
        + ":  "
        + str(
            len(
                shp.Vertexes
                + shp.Edges
                + shp.Wires
                + shp.Faces
                + shp.Shells
                + shp.Solids
                + shp.CompSolids
                + shp.Compounds
            )
        )
        + "\n"
    )
    if advancedShapeContent:
        result += "----------\n"
        if (
            hasattr(shp, "Area")
            and not "Wire" in typeStr
            and not "Edge" in typeStr
            and not "Vertex" in typeStr
        ):
            result += (
                translate("TaskCheckGeometryResults", "Area")
                + ":  "
                + str(round(shp.Area, decimals))
                + "\n"
            )
        if (
            hasattr(shp, "Volume")
            and not "Wire" in typeStr
            and not "Edge" in typeStr
            and not "Vertex" in typeStr
            and not "Face" in typeStr
        ):
            result += (
                translate("TaskCheckGeometryResults", "Volume")
                + ":  "
                + str(round(shp.Volume, decimals))
                + "\n"
            )
        if hasattr(shp, "Mass"):
            result += (
                translate("TaskCheckGeometryResults", "Mass")
                + ":  "
                + str(round(shp.Mass, decimals))
                + "\n"
            )
        if hasattr(shp, "Length"):
            result += (
                translate("TaskCheckGeometryResults", "Length")
                + ":  "
                + str(round(shp.Length, decimals))
                + "\n"
            )
        if hasattr(shp, "Curve") and hasattr(shp.Curve, "Radius"):
            result += (
                translate("TaskCheckGeometryResults", "Radius")
                + ":  "
                + str(round(shp.Curve.Radius, decimals))
                + "\n"
            )
        if hasattr(shp, "Curve") and hasattr(shp.Curve, "Center"):
            result += (
                translate("TaskCheckGeometryResults", "Curve center")
                + ":  "
                + str([round(vv, decimals) for vv in shp.Curve.Center])
                + "\n"
            )
        if hasattr(shp, "Curve") and hasattr(shp.Curve, "Continuity"):
            result += (
                translate("TaskCheckGeometryResults", "Continuity")
                + ":  "
                + str(shp.Curve.Continuity)
                + "\n"
            )
        if hasattr(shp, "CenterOfMass"):
            result += (
                translate("TaskCheckGeometryResults", "Center of mass")
                + ":  "
                + roundVector(shp.CenterOfMass, decimals)
                + "\n"
            )
        if hasattr(shp, "normalAt"):
            try:
                result += (
                    "normalAt(0):  " + str([round(vv, decimals) for vv in shp.normalAt(0)]) + "\n"
                )
            except Exception:
                try:
                    result += (
                        "normalAt(0,0):  "
                        + str([round(vv, decimals) for vv in shp.normalAt(0, 0)])
                        + "\n"
                    )
                except Exception:
                    pass
        if hasattr(shp, "isClosed") and ("Wire" in typeStr or "Edge" in typeStr):
            result += (
                translate("TaskCheckGeometryResults", "Is closed")
                + "?  "
                + str(shp.isClosed())
                + "\n"
            )
        if hasattr(shp, "Orientation"):
            result += (
                translate("TaskCheckGeometryResults", "Orientation")
                + ":  "
                + str(shp.Orientation)
                + "\n"
            )
        if hasattr(shp, "PrincipalProperties"):
            props = shp.PrincipalProperties
            for p in props:
                if isinstance(props[p], App.Vector) or isinstance(props[p], tuple):
                    result += str(p) + ":  " + roundVector(props[p], decimals) + "\n"
                else:
                    result += str(p) + ":  " + str(props[p]) + "\n"
        if hasattr(obj, "getGlobalPlacement"):
            if obj.getGlobalPlacement() != obj.Placement:
                rpl = obj.getGlobalPlacement() * obj.Placement.inverse()
                rot = rpl.Rotation
                if hasattr(shp, "CenterOfMass"):
                    result += (
                        translate("TaskCheckGeometryResults", "Global center of mass")
                        + ":  "
                        + roundVector(rpl.multVec(shp.CenterOfMass), decimals)
                        + "\n"
                    )
                if hasattr(shp, "PrincipalProperties"):
                    props = shp.PrincipalProperties
                    for p in props:
                        if "AxisOfInertia" in p:
                            result += (
                                "Global "
                                + str(p)
                                + ":  "
                                + roundVector(rot.multVec(props[p]), decimals)
                                + "\n"
                            )
            else:
                result += (
                    translate("TaskCheckGeometryResults", "Global placement")
                    + " = "
                    + translate("TaskCheckGeometryResults", "Placement")
                )
    return result