framework/cad_stability_check/main.py
2023-07-02 19:34:15 +03:00

79 lines
3.3 KiB
Python

import FreeCAD as App
import json
import re
from pyquaternion import Quaternion
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'
def computedStabiliti(refElement, childElement):
b = childElement.Shape.BoundBox
App.activeDocument().addObject("Part::MultiCommon", "Common")
App.activeDocument().Common.Shapes = [refElement, childElement, ]
App.ActiveDocument.getObject('Common').ViewObject.ShapeColor = getattr(App.getDocument('cubes').getObject(
refElement.Name).getLinkedObject(True).ViewObject, 'ShapeColor', App.getDocument('cubes').getObject('Common').ViewObject.ShapeColor)
App.ActiveDocument.getObject('Common').ViewObject.DisplayMode = getattr(App.getDocument('cubes').getObject(
childElement.Name).getLinkedObject(True).ViewObject, 'DisplayMode', App.getDocument('cubes').getObject('Common').ViewObject.DisplayMode)
App.ActiveDocument.recompute()
obj = App.ActiveDocument.getObjectsByLabel('Common')[0]
shp = obj.Shape
bbox = shp.BoundBox
if bbox.XLength == b.XLength and bbox.YLength == b.YLength and b.ZLength == bbox.ZLength:
return True
return False
def main():
App.newDocument()
env = json.loads((open('./env.json')).read())
coordinatsFilePath = env.get('pathToTheSimulationCoordinatesFile')
assemblyFolder = env.get('generationFolder')
buildNumber = int(re.findall(r'\d', coordinatsFilePath)[0])
assemblyStructure = json.loads(
(open(assemblyFolder + 'step-structure.json')).read())
assemblyNumber = int(buildNumber)
activeDetail = assemblyStructure[assemblyNumber]
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)
coordinats = json.loads((open(coordinatsFilePath)).read())
inc = 1
for el in App.ActiveDocument.Objects:
pos = coordinats[inc]['position']
qua = coordinats[inc]['quaternion']
new_quaternion = Quaternion(qua[0], qua[1], qua[2], qua[3])
rotation_matrix = new_quaternion.rotation_matrix
current_position = el.Placement.Base
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]))
print(computedStabiliti(meshMark, meshDetailOfTheMarkZone))
pass
main()