rdmax 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rdmax/__init__.py +18 -0
- rdmax/_common.py +31 -0
- rdmax/compat.py +20 -0
- rdmax/license.py +36 -0
- rdmax/mfn.py +357 -0
- rdmax/modeling.py +1674 -0
- rdmax/post.py +479 -0
- rdmax/rplt.py +320 -0
- rdmax/start.py +83 -0
- rdmax/textfile.py +302 -0
- rdmax/utils.py +89 -0
- rdmax-0.1.0.dist-info/METADATA +76 -0
- rdmax-0.1.0.dist-info/RECORD +16 -0
- rdmax-0.1.0.dist-info/WHEEL +5 -0
- rdmax-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
- rdmax-0.1.0.dist-info/top_level.txt +1 -0
rdmax/rplt.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
__all__ = ["RpltReader"]
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import os
|
|
5
|
+
import struct
|
|
6
|
+
|
|
7
|
+
class RpltReader:
|
|
8
|
+
"""
|
|
9
|
+
RecurDyn Plot Data Reader class
|
|
10
|
+
"""
|
|
11
|
+
def __init__(self, file_path=None):
|
|
12
|
+
self.__file_path = file_path
|
|
13
|
+
|
|
14
|
+
self.__sub_number = 0
|
|
15
|
+
self.__data_number = 0
|
|
16
|
+
self.__total_index = 0
|
|
17
|
+
|
|
18
|
+
self.__sel_index = [-1, -1, -1, -1]
|
|
19
|
+
self.__root_node = None
|
|
20
|
+
self.__sub_node = None
|
|
21
|
+
self.__parent_flag = False
|
|
22
|
+
self.__match_flag = False
|
|
23
|
+
self.__component_count = 0
|
|
24
|
+
self.__node_dim = 0
|
|
25
|
+
|
|
26
|
+
self.__header_end_position = None
|
|
27
|
+
|
|
28
|
+
self.__unit = None
|
|
29
|
+
|
|
30
|
+
if self.__file_path:
|
|
31
|
+
self.import_file(self.__file_path)
|
|
32
|
+
|
|
33
|
+
def _get_unit(self):
|
|
34
|
+
return self.unit
|
|
35
|
+
|
|
36
|
+
unit = property(_get_unit, None)
|
|
37
|
+
|
|
38
|
+
class __Node:
|
|
39
|
+
"""
|
|
40
|
+
Inner class for rplt header
|
|
41
|
+
"""
|
|
42
|
+
def __init__(self, name, text):
|
|
43
|
+
self.name = name
|
|
44
|
+
self.text = text
|
|
45
|
+
self.image_key = "-1"
|
|
46
|
+
self.nodes = []
|
|
47
|
+
self.next_node = None
|
|
48
|
+
self.parent = None
|
|
49
|
+
|
|
50
|
+
def add_node(self, node):
|
|
51
|
+
node.parent = self
|
|
52
|
+
previous_node_idx = len(self.nodes) - 1
|
|
53
|
+
if previous_node_idx != -1:
|
|
54
|
+
self.nodes[previous_node_idx].next_node = node
|
|
55
|
+
self.nodes.append(node)
|
|
56
|
+
|
|
57
|
+
def get_dim_parent_node(self, node_dim):
|
|
58
|
+
parent_node = self
|
|
59
|
+
for dim in range(node_dim):
|
|
60
|
+
parent_node = parent_node.parent
|
|
61
|
+
if not parent_node:
|
|
62
|
+
break
|
|
63
|
+
else:
|
|
64
|
+
return parent_node
|
|
65
|
+
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
def _get_child_name_list_recursive(self, name_list):
|
|
69
|
+
child_name_list = []
|
|
70
|
+
for node in self.nodes:
|
|
71
|
+
node._get_child_name_list_recursive(child_name_list)
|
|
72
|
+
if not child_name_list:
|
|
73
|
+
name_list.append(self.text)
|
|
74
|
+
else:
|
|
75
|
+
for child_name in child_name_list:
|
|
76
|
+
name = self.text + "/" + child_name
|
|
77
|
+
name_list.append(name)
|
|
78
|
+
child_name_list.clear()
|
|
79
|
+
|
|
80
|
+
def get_child_name_list(self):
|
|
81
|
+
name_list = []
|
|
82
|
+
for node in self.nodes:
|
|
83
|
+
node._get_child_name_list_recursive(name_list)
|
|
84
|
+
if not name_list:
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
return name_list
|
|
88
|
+
|
|
89
|
+
def _read_binary_int32(self, f):
|
|
90
|
+
return int.from_bytes(f.read(4), byteorder='little')
|
|
91
|
+
|
|
92
|
+
def _read_binary_int8(self, f):
|
|
93
|
+
return int.from_bytes(f.read(1), byteorder='little')
|
|
94
|
+
|
|
95
|
+
def _read_binary_double(self, f):
|
|
96
|
+
return struct.unpack('<d', f.read(8))[0]
|
|
97
|
+
|
|
98
|
+
def _read_binary_string(self, f):
|
|
99
|
+
_ = f.read(3)
|
|
100
|
+
count = self._read_binary_int8(f)
|
|
101
|
+
data = f.read(count*2)
|
|
102
|
+
return data.decode('utf-16')
|
|
103
|
+
|
|
104
|
+
def _read_header(self, file_path):
|
|
105
|
+
self.__file_path = file_path
|
|
106
|
+
|
|
107
|
+
self.__sub_number = 0
|
|
108
|
+
self.__data_number = 0
|
|
109
|
+
self.__total_index = 0
|
|
110
|
+
|
|
111
|
+
with open(file_path, 'rb') as f:
|
|
112
|
+
version = self._read_binary_int32(f)
|
|
113
|
+
if version == 2:
|
|
114
|
+
version = 1
|
|
115
|
+
|
|
116
|
+
recurdyn_pro_sdk = self._read_binary_int32(f)
|
|
117
|
+
recurdyn_version = self._read_binary_int32(f)
|
|
118
|
+
|
|
119
|
+
f.read(8)
|
|
120
|
+
|
|
121
|
+
self.__unit = [self._read_binary_string(f) for i in range(5)]
|
|
122
|
+
|
|
123
|
+
model_name = self._read_binary_string(f)
|
|
124
|
+
self.__root_node = self.__Node("", model_name)
|
|
125
|
+
|
|
126
|
+
entity_count = self._read_binary_int32(f)
|
|
127
|
+
for i in range(entity_count):
|
|
128
|
+
self.__node_dim = 0
|
|
129
|
+
self._read_plot_recursive(f, version, self.__root_node)
|
|
130
|
+
|
|
131
|
+
self.__header_end_position = f.tell()
|
|
132
|
+
f.close()
|
|
133
|
+
|
|
134
|
+
self.__sub_node = None
|
|
135
|
+
|
|
136
|
+
def _read_plot_recursive(self, f, version, node):
|
|
137
|
+
self.__sub_number += 1
|
|
138
|
+
dimension_type = 0
|
|
139
|
+
|
|
140
|
+
name = self._read_binary_string(f)
|
|
141
|
+
entity_count = self._read_binary_int32(f)
|
|
142
|
+
|
|
143
|
+
if version >= 1:
|
|
144
|
+
if entity_count <= 0:
|
|
145
|
+
dimension_type = self._read_binary_int32(f)
|
|
146
|
+
if dimension_type == -1 or dimension_type == 4294967295: # dimension_type_user_define
|
|
147
|
+
_ = self._read_binary_string(f)
|
|
148
|
+
|
|
149
|
+
self.__sub_node = self.__Node(str(dimension_type), name)
|
|
150
|
+
if entity_count > 0:
|
|
151
|
+
if self.__parent_flag and node is not None and node.parent is not None:
|
|
152
|
+
dim_parent_node = node.get_dim_parent_node(self.__node_dim)
|
|
153
|
+
dim_parent_node.add_node(self.__sub_node)
|
|
154
|
+
else:
|
|
155
|
+
node.add_node(self.__sub_node)
|
|
156
|
+
|
|
157
|
+
self.__parent_flag = False
|
|
158
|
+
for i in range(entity_count):
|
|
159
|
+
self._read_plot_recursive(f, version, self.__sub_node)
|
|
160
|
+
self.__parent_flag = True
|
|
161
|
+
self.__node_dim += 1
|
|
162
|
+
|
|
163
|
+
else:
|
|
164
|
+
image_key = str(entity_count)
|
|
165
|
+
if entity_count == 0:
|
|
166
|
+
image_key = str(self.__data_number)
|
|
167
|
+
self.__total_index = self.__data_number
|
|
168
|
+
self.__data_number += 1
|
|
169
|
+
self.__node_dim = 0
|
|
170
|
+
|
|
171
|
+
self.__sub_node.image_key = image_key
|
|
172
|
+
|
|
173
|
+
if node.parent is None:
|
|
174
|
+
node.add_node(self.__sub_node)
|
|
175
|
+
else:
|
|
176
|
+
node.add_node(self.__sub_node)
|
|
177
|
+
self.__sub_node = node
|
|
178
|
+
|
|
179
|
+
self.__sub_number -= 1
|
|
180
|
+
|
|
181
|
+
def _find_component(self, list_find):
|
|
182
|
+
self.__sel_index = [-1, -1, -1, -1]
|
|
183
|
+
|
|
184
|
+
self.__match_flag = False
|
|
185
|
+
self.__component_count = 0
|
|
186
|
+
self._find_component_recursive(self.__root_node, list_find)
|
|
187
|
+
|
|
188
|
+
def _find_component_recursive(self, node, list_find):
|
|
189
|
+
len_node_nodes = len(node.nodes)
|
|
190
|
+
if len_node_nodes == 0:
|
|
191
|
+
self.__match_flag = True
|
|
192
|
+
c_index = int(node.image_key)
|
|
193
|
+
if c_index >= 0:
|
|
194
|
+
self.__sel_index[0] = c_index
|
|
195
|
+
if c_index == -2:
|
|
196
|
+
self.__sel_index[0] = c_index
|
|
197
|
+
self.__sel_index[1] = int(node.next_node.image_key)
|
|
198
|
+
self.__sel_index[2] = int(node.next_node.next_node.image_key)
|
|
199
|
+
if c_index == -3:
|
|
200
|
+
self.__sel_index[0] = c_index
|
|
201
|
+
self.__sel_index[1] = int(node.next_node.image_key)
|
|
202
|
+
self.__sel_index[2] = int(node.next_node.next_node.image_key)
|
|
203
|
+
self.__sel_index[2] = int(node.next_node.next_node.next_node.image_key)
|
|
204
|
+
elif len_node_nodes > 0:
|
|
205
|
+
for child_node in node.nodes:
|
|
206
|
+
if self.__match_flag:
|
|
207
|
+
break
|
|
208
|
+
|
|
209
|
+
child_node_text = child_node.text
|
|
210
|
+
if child_node_text[-1] == " ":
|
|
211
|
+
child_node_text = child_node_text[:-1]
|
|
212
|
+
if list_find[self.__component_count] == child_node_text:
|
|
213
|
+
self.__component_count += 1
|
|
214
|
+
self._find_component_recursive(child_node, list_find)
|
|
215
|
+
|
|
216
|
+
def _read_data_from_rplt(self, list_component):
|
|
217
|
+
data = None
|
|
218
|
+
self._find_component(list_component)
|
|
219
|
+
if self.__sel_index[0] >= 0:
|
|
220
|
+
data = self._read_data(self.__header_end_position, self.__sel_index[0], self.__total_index)
|
|
221
|
+
elif self.__sel_index[0] == -2:
|
|
222
|
+
x_data = self._read_data(self.__header_end_position, self.__sel_index[1], self.__total_index)
|
|
223
|
+
y_data = self._read_data(self.__header_end_position, self.__sel_index[2], self.__total_index)
|
|
224
|
+
|
|
225
|
+
len_x_data = len(x_data)
|
|
226
|
+
data = [math.sqrt(x_data[idx] ** 2 + y_data[idx] ** 2) for idx in range(len_x_data)]
|
|
227
|
+
|
|
228
|
+
x_data.clear()
|
|
229
|
+
y_data.clear()
|
|
230
|
+
elif self.__sel_index[0] == -3:
|
|
231
|
+
x_data = self._read_data(self.__header_end_position, self.__sel_index[1], self.__total_index)
|
|
232
|
+
y_data = self._read_data(self.__header_end_position, self.__sel_index[2], self.__total_index)
|
|
233
|
+
z_data = self._read_data( self.__header_end_position, self.__sel_index[3], self.__total_index)
|
|
234
|
+
|
|
235
|
+
len_x_data = len(x_data)
|
|
236
|
+
data = [math.sqrt(x_data[idx]**2 + y_data[idx]**2 + z_data[idx]**2) for idx in range(len_x_data)]
|
|
237
|
+
|
|
238
|
+
x_data.clear()
|
|
239
|
+
y_data.clear()
|
|
240
|
+
z_data.clear()
|
|
241
|
+
|
|
242
|
+
return data
|
|
243
|
+
|
|
244
|
+
def _read_data(self, data_start_position, data_index, total_index):
|
|
245
|
+
data = None
|
|
246
|
+
with open(self.__file_path, 'rb') as f:
|
|
247
|
+
current_data_position = data_start_position + data_index * 8
|
|
248
|
+
|
|
249
|
+
f.seek(0, os.SEEK_END)
|
|
250
|
+
data_length = f.tell()
|
|
251
|
+
|
|
252
|
+
f.seek(current_data_position, 0)
|
|
253
|
+
|
|
254
|
+
data_size = data_length - data_start_position
|
|
255
|
+
data_size = int(data_size / (8 * (total_index+1)))
|
|
256
|
+
|
|
257
|
+
data_offset = 8 * total_index
|
|
258
|
+
data = [0.0] * data_size
|
|
259
|
+
for idx in range(data_size):
|
|
260
|
+
data[idx] = self._read_binary_double(f)
|
|
261
|
+
f.seek(data_offset, 1)
|
|
262
|
+
|
|
263
|
+
f.close()
|
|
264
|
+
|
|
265
|
+
return data
|
|
266
|
+
|
|
267
|
+
def clear(self):
|
|
268
|
+
"""
|
|
269
|
+
Clear memory
|
|
270
|
+
"""
|
|
271
|
+
self.__file_path = None
|
|
272
|
+
|
|
273
|
+
self.__sub_number = 0
|
|
274
|
+
self.__data_number = 0
|
|
275
|
+
self.__total_index = 0
|
|
276
|
+
|
|
277
|
+
self.__sel_index = [-1, -1, -1, -1]
|
|
278
|
+
|
|
279
|
+
if self.__root_node:
|
|
280
|
+
self.__root_node.clear_all()
|
|
281
|
+
self.__root_node = None
|
|
282
|
+
|
|
283
|
+
self.__sub_node = None
|
|
284
|
+
self.__parent_flag = False
|
|
285
|
+
self.__match_flag = False
|
|
286
|
+
self.__component_count = 0
|
|
287
|
+
self.__node_dim = 0
|
|
288
|
+
|
|
289
|
+
self.__header_end_position = None
|
|
290
|
+
|
|
291
|
+
if self.__unit:
|
|
292
|
+
self.__unit.clear()
|
|
293
|
+
self.__unit = None
|
|
294
|
+
|
|
295
|
+
def import_file(self, file_path):
|
|
296
|
+
"""
|
|
297
|
+
Import *.rplt file.
|
|
298
|
+
|
|
299
|
+
:param file_path: str
|
|
300
|
+
"""
|
|
301
|
+
self._read_header(file_path)
|
|
302
|
+
|
|
303
|
+
def get_pot_data(self, string_component):
|
|
304
|
+
"""Alias of get_plot_data (kept for old scripts)."""
|
|
305
|
+
return self.get_plot_data(string_component)
|
|
306
|
+
|
|
307
|
+
def get_plot_data(self, string_component):
|
|
308
|
+
"""
|
|
309
|
+
Input the data name to be imported as follows.
|
|
310
|
+
"Bodies/Body1/Pos_TM"
|
|
311
|
+
|
|
312
|
+
:param file_path: str
|
|
313
|
+
"""
|
|
314
|
+
if self.__file_path is None:
|
|
315
|
+
raise Exception("Please import the file first.")
|
|
316
|
+
list_component = string_component.replace('\\', '/').split('/')
|
|
317
|
+
return self._read_data_from_rplt(list_component)
|
|
318
|
+
|
|
319
|
+
def get_plottable_name_list(self):
|
|
320
|
+
return self.__root_node.get_child_name_list()
|
rdmax/start.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Select a RecurDyn instance by model path, or start a new one."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
import time
|
|
5
|
+
import winreg
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
__all__ = ["start_recurdyn"]
|
|
9
|
+
|
|
10
|
+
import pythoncom
|
|
11
|
+
import win32com.client as win32
|
|
12
|
+
import win32gui
|
|
13
|
+
import win32process
|
|
14
|
+
|
|
15
|
+
from recurdyn.ProcessNet import IApplication, IModelDocument, PathType
|
|
16
|
+
from recurdyn.RecurDyn import IRecurDynApp
|
|
17
|
+
from recurdyn.utils.GetComObjectByROT import get_clsid_from_progid, get_com_object_by_guid
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def start_recurdyn(version, model_path):
|
|
21
|
+
"""Return the application and document for the requested model."""
|
|
22
|
+
model_path = Path(model_path).expanduser().resolve()
|
|
23
|
+
|
|
24
|
+
with winreg.OpenKey(
|
|
25
|
+
winreg.HKEY_LOCAL_MACHINE,
|
|
26
|
+
rf"SOFTWARE\FunctionBay, Inc.\RecurDyn {version}\Path",
|
|
27
|
+
) as key:
|
|
28
|
+
root = Path(winreg.QueryValueEx(key, "ROOT")[0]).resolve()
|
|
29
|
+
|
|
30
|
+
import recurdyn
|
|
31
|
+
package_dir = (root / "Bin" / "Python" / "Lib" / "recurdyn").resolve()
|
|
32
|
+
pip_year = getattr(recurdyn, "RECURDYN_YEAR", None) # set only in pip-built recurdyn packages
|
|
33
|
+
if pip_year is not None:
|
|
34
|
+
if str(pip_year) != str(version).split()[0]:
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
f"The installed recurdyn package is for RecurDyn {pip_year}, but RecurDyn {version} was requested.")
|
|
37
|
+
elif Path(recurdyn.__file__).resolve().parent != package_dir:
|
|
38
|
+
raise RuntimeError("The imported recurdyn package does not match the requested version.")
|
|
39
|
+
|
|
40
|
+
clsid = get_clsid_from_progid("RecurDyn.RDApplication")
|
|
41
|
+
if clsid is None:
|
|
42
|
+
raise RuntimeError("RecurDyn COM registration was not found.")
|
|
43
|
+
process = None
|
|
44
|
+
|
|
45
|
+
while True:
|
|
46
|
+
for name, unknown in get_com_object_by_guid(f"!{clsid}:").items():
|
|
47
|
+
try:
|
|
48
|
+
hwnd = int(name.rsplit(":", 1)[-1])
|
|
49
|
+
if not win32gui.IsWindow(hwnd):
|
|
50
|
+
continue
|
|
51
|
+
if process and win32process.GetWindowThreadProcessId(hwnd)[1] != process.pid:
|
|
52
|
+
continue
|
|
53
|
+
|
|
54
|
+
rd = IRecurDynApp(win32.Dispatch(unknown.QueryInterface(pythoncom.IID_IDispatch)))
|
|
55
|
+
application = IApplication(rd.RecurDynApplication)
|
|
56
|
+
installed = Path(application.GetInstallationPath()).resolve()
|
|
57
|
+
if installed != root and root not in installed.parents:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
if process:
|
|
61
|
+
if model_path.is_file():
|
|
62
|
+
modelDocument = IModelDocument(application.OpenModelDocument(str(model_path)))
|
|
63
|
+
else:
|
|
64
|
+
modelDocument = IModelDocument(application.NewModelDocument(model_path.stem))
|
|
65
|
+
if not modelDocument.FileSave(str(model_path), True):
|
|
66
|
+
raise RuntimeError(f"Failed to save model: {model_path}")
|
|
67
|
+
else:
|
|
68
|
+
opened = application.GetModelDocument(model_path.name)
|
|
69
|
+
if not opened or Path(opened.GetPath(PathType.WorkingDocument)).resolve() != model_path:
|
|
70
|
+
continue
|
|
71
|
+
modelDocument = IModelDocument(opened)
|
|
72
|
+
|
|
73
|
+
print(f"RecurDyn {application.Version} - rdyn: {modelDocument.Name}")
|
|
74
|
+
return application, modelDocument
|
|
75
|
+
except (ValueError, pythoncom.com_error):
|
|
76
|
+
continue
|
|
77
|
+
|
|
78
|
+
if process is None:
|
|
79
|
+
process = subprocess.Popen([str(root / "Bin" / "RecurDyn.exe")])
|
|
80
|
+
deadline = time.monotonic() + 300
|
|
81
|
+
elif process.poll() is not None or time.monotonic() > deadline:
|
|
82
|
+
raise RuntimeError("Failed to connect to the new RecurDyn instance.")
|
|
83
|
+
time.sleep(1)
|