File size: 5,801 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
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2025 Mario Passaglia <mpassaglia[at]cbc.uba.ar>         *
# *                                                                         *
# *   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/>.                                      *
# *                                                                         *
# ***************************************************************************

__title__ = "FreeCAD FEM constraint electric charge density document object"
__author__ = "Mario Passaglia"
__url__ = "https://www.freecad.org"

## @package constraint_electricchargedensity
#  \ingroup FEM
#  \brief constraint electric charge density object


from FreeCAD import Units
from FreeCAD import Base

from . import base_fempythonobject

_PropHelper = base_fempythonobject._PropHelper


class ConstraintElectricChargeDensity(base_fempythonobject.BaseFemPythonObject):

    Type = "Fem::ConstraintElectricChargeDensity"

    def __init__(self, obj):
        super().__init__(obj)

        for prop in self._get_properties():
            prop.add_to_object(obj)

    def _get_properties(self):
        prop = []

        prop.append(
            _PropHelper(
                type="App::PropertyVolumeChargeDensity",
                name="SourceChargeDensity",
                group="Electric Charge Density",
                doc="Free electric charge per unit volume at the sources",
                value="0 C/mm^3",
            )
        )
        prop.append(
            _PropHelper(
                type="App::PropertySurfaceChargeDensity",
                name="InterfaceChargeDensity",
                group="Electric Charge Density",
                doc="Free electric charge per unit surface at the boundaries",
                value="0 C/mm^2",
            )
        )
        prop.append(
            _PropHelper(
                type="App::PropertyElectricCharge",
                name="TotalCharge",
                group="Electric Charge Density",
                doc="Total free electric charge",
                value="0 C",
            )
        )
        prop.append(
            _PropHelper(
                type="App::PropertyBool",
                name="Concentrated",
                group="Electric Charge Density",
                doc="Use concentrated charges at the nodes",
                value=False,
            )
        )
        prop.append(
            _PropHelper(
                type="App::PropertyEnumeration",
                name="Mode",
                group="Electric Charge Density",
                doc="Switch quantity input mode",
                value=["Interface", "Source", "Total Interface", "Total Source"],
            )
        )

        return prop

    def onDocumentRestored(self, obj):
        print("restored", obj.Name)
        # update old project with new properties
        for prop in self._get_properties():
            try:
                obj.getPropertyByName(prop.name)
            except Base.PropertyError:
                prop.add_to_object(obj)

    def onChanged(self, obj, prop):
        print("change", prop)
        if prop == "Mode":
            if obj.Mode == "Total Source":
                obj.setPropertyStatus("Concentrated", "-ReadOnly")
            else:
                obj.setPropertyStatus("Concentrated", "ReadOnly")

    def get_total_source_density(self, obj):
        """
        Calculate density for `Total Source` mode.
        """
        size = 0
        items = []
        for feat, sub_elem in obj.References:
            for name in sub_elem:
                sub = feat.getSubObject(name)
                if sub.ShapeType == "Solid":
                    size += sub.Volume
                    items.append(name)
                elif sub.ShapeType == "Face":
                    size += sub.Area
                    items.append(name)

        if items:
            vol = Units.Quantity(f"{size} mm^3")
            return obj.TotalCharge / vol

    def get_total_interface_density(self, obj):
        """
        Calculate density for `Total Interface` mode.
        """
        size = 0
        items = []
        for feat, sub_elem in obj.References:
            for name in sub_elem:
                sub = feat.getSubObject(name)
                if sub.ShapeType == "Face":
                    size += sub.Area
                    items.append(name)
                elif sub.ShapeType == "Edge":
                    size += sub.Length
                    items.append(name)

        if items:
            area = Units.Quantity(f"{size} mm^2")
            return obj.TotalCharge / area