framework/cad_stability_check/main.py

93 lines
4.3 KiB
Python
Raw Normal View History

import FreeCAD as App
import json
import re
from pyquaternion import Quaternion
2023-07-05 16:03:51 +03:00
def importObjAtPath(path: str):
import importOBJ
importOBJ.insert(u"" + path, App.ActiveDocument.Label)
pass
def getFullPathObj(assemblyFolder: str, name: str):
return assemblyFolder + 'sdf/meshes/' + name + '.obj'
2023-07-05 16:03:51 +03:00
def computedStability(refElement, childElement):
rootElement = childElement.Shape.BoundBox
# Создание обьекта на котором делается операция пересечения
App.activeDocument().addObject("Part::MultiCommon", "Common")
App.activeDocument().Common.Shapes = [refElement, childElement, ]
App.ActiveDocument.getObject('Common').ViewObject.ShapeColor = getattr(App.ActiveDocument.getObject(
refElement.Name).getLinkedObject(True).ViewObject, 'ShapeColor', App.ActiveDocument.getObject('Common').ViewObject.ShapeColor)
App.ActiveDocument.getObject('Common').ViewObject.DisplayMode = getattr(App.ActiveDocument.getObject(
childElement.Name).getLinkedObject(True).ViewObject, 'DisplayMode', App.ActiveDocument.getObject('Common').ViewObject.DisplayMode)
App.ActiveDocument.recompute()
obj = App.ActiveDocument.getObjectsByLabel('Common')[0]
shp = obj.Shape
bbox = shp.BoundBox
# Если после операции пересечения зона обьекта совпадает с зоной тестируемого обьекта то тест прошел успешно
if bbox.XLength == rootElement.XLength and bbox.YLength == rootElement.YLength and rootElement.ZLength == bbox.ZLength:
return True
return False
def main():
App.newDocument()
2023-07-05 16:03:51 +03:00
# загрузка env интерфейса
env = json.loads((open('./env.json')).read())
2023-07-05 16:03:51 +03:00
# получение пути координат файла
coordinatesFilePath = env.get('pathToTheSimulationCoordinatesFile')
# получение папки сборки
assemblyFolder = env.get('generationFolder')
2023-07-05 16:03:51 +03:00
buildNumber = int(re.findall(r'\d', coordinatesFilePath)[0])
2023-07-05 16:03:51 +03:00
# загрузка последовательности сборки ввиде списка строк деталей
assemblyStructure = json.loads(
(open(assemblyFolder + 'step-structure.json')).read())
2023-07-05 16:03:51 +03:00
# получение номера тестируемой сборки
assemblyNumber = int(buildNumber)
2023-07-05 16:03:51 +03:00
# получение тестируемой детали в сборке
activeDetail = assemblyStructure[assemblyNumber]
2023-07-05 16:03:51 +03:00
subassemblyNotParticipatingInMarkup = assemblyStructure[0:assemblyNumber - 1]
detailOfTheMarkingZoneOfWhich = assemblyStructure[assemblyNumber - 1]
importObjAtPath(getFullPathObj(assemblyFolder, activeDetail))
importObjAtPath(getFullPathObj(
assemblyFolder, detailOfTheMarkingZoneOfWhich))
meshMark = App.ActiveDocument.Objects[0]
meshDetailOfTheMarkZone = App.ActiveDocument.Objects[1]
meshMark.ViewObject.ShapeColor = (0.31, 0.77, 0.87)
meshDetailOfTheMarkZone.ViewObject.ShapeColor = (0.68, 0.66, 0.95)
for el in list(map(lambda el: getFullPathObj(assemblyFolder, el), subassemblyNotParticipatingInMarkup)):
importObjAtPath(el)
for el in App.ActiveDocument.Objects[2:App.ActiveDocument.Objects.__len__()]:
el.ViewObject.ShapeColor = (0.32, 0.05, 0.38)
2023-07-05 16:03:51 +03:00
# загрузка координат
coordinates = json.loads((open(coordinatesFilePath)).read())
inc = 1
2023-07-05 16:03:51 +03:00
# перемещение обьектов в документе на точки стабильность полученные из pybullet
for el in App.ActiveDocument.Objects:
2023-07-05 16:03:51 +03:00
pos = coordinates[inc]['position']
qua = coordinates[inc]['quaternion']
new_quaternion = Quaternion(qua[0], qua[1], qua[2], qua[3])
rotation_matrix = new_quaternion.rotation_matrix
current_position = el.Placement.Base
2023-07-05 16:03:51 +03:00
new_position = App.Vector(
current_position.x, current_position.y, current_position.z)
new_placement = App.Placement(new_position, rotation_matrix)
el.Placement = new_placement
App.ActiveDocument.recompute()
el.Placement.move(App.Vector(pos[0], pos[1], pos[2]))
2023-07-05 16:03:51 +03:00
# вычисление стабильность
computedStability(meshMark, meshDetailOfTheMarkZone)
pass
main()