Compare commits
3 Commits
b4eb9b7207
...
1fe426486e
Author | SHA1 | Date | |
---|---|---|---|
1fe426486e | |||
10c787ba55 | |||
c5795254bb |
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>assembly_handbook</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.python.pydev.PyDevBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.python.pydev.pythonNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
21
.pydevproject
Normal file
21
.pydevproject
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
|
||||
|
||||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
|
||||
|
||||
|
||||
<path>/${PROJECT_DIR_NAME}</path>
|
||||
|
||||
|
||||
</pydev_pathproperty>
|
||||
|
||||
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
|
||||
|
||||
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||
|
||||
|
||||
|
||||
</pydev_project>
|
36
InitGui.py
36
InitGui.py
@ -20,6 +20,8 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
|
||||
dev = True # indicates development mode (enables additional tools, log, debug checks, etc.)
|
||||
|
||||
context = None
|
||||
|
||||
docObserver = None
|
||||
|
||||
def GetClassName(self):
|
||||
return "Gui::PythonWorkbench"
|
||||
@ -37,6 +39,11 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
|
||||
if self.context is None:
|
||||
self.initializeContext()
|
||||
|
||||
import ahb_document_observer
|
||||
if self.docObserver is None:
|
||||
self.docObserver = ahb_document_observer.DocObserver()
|
||||
App.addDocumentObserver(self.docObserver)
|
||||
|
||||
def Deactivated(self):
|
||||
"""
|
||||
code which should be computed when this workbench is deactivated
|
||||
@ -50,31 +57,28 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
|
||||
def registerCommands(self):
|
||||
toolbox = []
|
||||
|
||||
self.importModule('ahb_cmd_export_csv')
|
||||
toolbox.append("AHB_exportCsv")
|
||||
#self.importModule('ahb_cmd_export_csv')
|
||||
#toolbox.append("AHB_exportCsv")
|
||||
|
||||
self.importModule('ahb_cmd_set_active_stage')
|
||||
toolbox.append("AHB_setActiveStage")
|
||||
#self.importModule('ahb_cmd_set_active_stage')
|
||||
#toolbox.append("AHB_setActiveStage")
|
||||
|
||||
self.importModule('ahb_cmd_set_part_stage')
|
||||
toolbox.append("AHB_setPartStage")
|
||||
#self.importModule('ahb_cmd_set_part_stage')
|
||||
#toolbox.append("AHB_setPartStage")
|
||||
|
||||
self.importModule('ahb_cmd_switch_visibility_mode')
|
||||
toolbox.append("AHB_switchVisibilityMode")
|
||||
#self.importModule('ahb_cmd_switch_visibility_mode')
|
||||
#toolbox.append("AHB_switchVisibilityMode")
|
||||
|
||||
self.importModule('ahb_cmd_render')
|
||||
toolbox.append("AHB_render")
|
||||
#self.importModule('ahb_cmd_render')
|
||||
#toolbox.append("AHB_render")
|
||||
|
||||
self.importModule('ahb_cmd_animate')
|
||||
toolbox.append("AHB_animate")
|
||||
|
||||
#self.importModule('ahb_cmd_animate')
|
||||
#toolbox.append("AHB_animate")
|
||||
|
||||
self.importModule('ahb_cmd_view_annotate')
|
||||
toolbox.append("AHB_view_annotate")
|
||||
|
||||
if self.dev:
|
||||
self.importModule('ahb_cmd_parse_step')
|
||||
toolbox.append("AHB_parse_step")
|
||||
|
||||
self.importModule('ahb_cmd_reload')
|
||||
toolbox.append("AHB_reload")
|
||||
|
||||
|
@ -1,191 +0,0 @@
|
||||
import FreeCADGui as Gui
|
||||
import FreeCAD as App
|
||||
|
||||
import re
|
||||
_object_reference_re = re.compile("^(.*?_)?((CHO|ELE|ROU|TSM|QIN|ACC|DIV|TXT|FRN|T|M|L|R)[0-9]+)(_[0-9]*)?$")
|
||||
_object_reference_clean_re = re.compile("^(.*?)(_st[0-9]+)?$")
|
||||
_bad_reference_re = re.compile("^[0-9]*$")
|
||||
_has_number_re = re.compile("_[0-9]+$")
|
||||
|
||||
class AHB_ParseStep:
|
||||
def GetResources(self):
|
||||
return {"MenuText": "[dev] parse STEP",
|
||||
"ToolTip": "Reload informations from the original STEP file",
|
||||
"Pixmap": ""
|
||||
}
|
||||
|
||||
def IsActive(self):
|
||||
return True
|
||||
|
||||
def Activated(self):
|
||||
import step_parser
|
||||
import os
|
||||
import importlib
|
||||
|
||||
importlib.reload(step_parser)
|
||||
|
||||
print("Reloading STEP file metadata...")
|
||||
|
||||
doc = App.activeDocument()
|
||||
|
||||
filename: str = doc.FileName
|
||||
if filename is None:
|
||||
raise BaseException("You must save your FreeCAD document before parsing the corresponding STEP file")
|
||||
|
||||
filename = os.path.splitext(filename)[0] + ".step"
|
||||
|
||||
step_info = step_parser.parse(filename)
|
||||
|
||||
objects = doc.Objects
|
||||
|
||||
override_names = False
|
||||
feature_idx = 0
|
||||
total_features = 0
|
||||
obj: App.DocumentObject
|
||||
for obj in objects:
|
||||
if obj.TypeId == 'Part::Feature':
|
||||
if "Base_OriginalLabel" in obj.PropertiesList:
|
||||
obj.Label = obj.Base_OriginalLabel
|
||||
|
||||
total_features = total_features + 1
|
||||
object_idx = -1
|
||||
if override_names:
|
||||
object_idx = feature_idx + 1
|
||||
else:
|
||||
fixed_name = step_parser.process_name(obj.Label)[0]
|
||||
for idx in range(len(step_info.objects)):
|
||||
if step_info.objects[idx].name == fixed_name:
|
||||
object_idx = idx
|
||||
break
|
||||
|
||||
if 0 <= object_idx < len(step_info.objects):
|
||||
object_info = step_info.objects[object_idx]
|
||||
print("phase 1 " + obj.Label + " => " + object_info.name)
|
||||
|
||||
obj.Label = object_info.name
|
||||
|
||||
if object_info.layer is not None:
|
||||
if "Base_Reference" not in obj.PropertiesList:
|
||||
obj.addProperty("App::PropertyString", "Base_Reference", "Base")
|
||||
obj.Base_Reference = object_info.layer.reference
|
||||
|
||||
if "Base_Layer" not in obj.PropertiesList:
|
||||
obj.addProperty("App::PropertyString", "Base_Layer", "Base")
|
||||
obj.Base_Layer = object_info.layer.name
|
||||
feature_idx = feature_idx + 1
|
||||
|
||||
step_info2 = step_parser.parse(filename, 'SOLIDS')
|
||||
print("STEP names phase 2 (" + str(len(step_info2.objects)) + ")")
|
||||
feature_idx = 0
|
||||
|
||||
# don't know why, but these objects are not ordered like the others
|
||||
special_objects = {
|
||||
'10_01_003 F_st7758_1': 'COMPOUND005', # manivelle droite
|
||||
'Batterie': 'COMPOUND058', # batterie basse
|
||||
'Batterie_1': 'COMPOUND059', # batterie haute
|
||||
'Axa compactline 35-E 6v-12v dc': 'COMPOUND1272', # phare droit
|
||||
'Axa compactline 35-E 6v-12v dc_1': 'COMPOUND1273', # phare gauche
|
||||
'Potence_CHO44': 'COMPOUND732', # potence
|
||||
'CHO04': 'COMPOUND1290', # guidon
|
||||
'CHO40': 'COMPOUND1291', # garde-boue droit
|
||||
'CHO41': 'COMPOUND1292', # garde-boue gauche
|
||||
'Pochette laterale': 'COMPOUND1294', # pochette gauche
|
||||
'Pochette laterale_1': 'COMPOUND1293', # pochette droite
|
||||
}
|
||||
|
||||
object_idx = -1
|
||||
|
||||
for obj in objects:
|
||||
if obj.TypeId == 'Part::Feature':
|
||||
if object_idx >= 0:
|
||||
object_idx = object_idx + 1
|
||||
|
||||
explicit_object_idx = -1
|
||||
for s in special_objects.items():
|
||||
if obj.Label == s[1]:
|
||||
for idx, info in enumerate(step_info2.objects):
|
||||
if info.name == s[0]:
|
||||
explicit_object_idx = idx
|
||||
|
||||
if explicit_object_idx >= 0:
|
||||
object_idx = explicit_object_idx
|
||||
else:
|
||||
while True:
|
||||
if object_idx < 0 or object_idx >= len(step_info2.objects):
|
||||
break
|
||||
object_info = step_info2.objects[object_idx]
|
||||
skip = False
|
||||
for s in special_objects.items():
|
||||
if s[0] == object_info.name:
|
||||
skip = True
|
||||
if skip:
|
||||
object_idx = object_idx + 1
|
||||
else:
|
||||
break
|
||||
|
||||
if 0 <= object_idx < len(step_info2.objects):
|
||||
object_info = step_info2.objects[object_idx]
|
||||
|
||||
if obj.Label.startswith("COMPOUND"):
|
||||
print("phase 2 " + obj.Label + " => " + object_info.name + " (ref="+object_info.layer.reference+", layer="+object_info.layer.name+")")
|
||||
|
||||
if "Base_OriginalLabel" not in obj.PropertiesList:
|
||||
obj.addProperty("App::PropertyString", "Base_OriginalLabel", "Base")
|
||||
obj.Base_OriginalLabel = obj.Label
|
||||
|
||||
final_ref = self.parse_vhelio_reference(object_info.name)
|
||||
if object_info.layer is not None:
|
||||
if final_ref is None:
|
||||
final_ref = self.parse_vhelio_reference(step_parser.process_name(object_info.layer.reference)[1])
|
||||
|
||||
if final_ref is None:
|
||||
final_ref = step_parser.process_name(object_info.layer.reference)[1]
|
||||
if object_info.name != final_ref and not object_info.name.startswith(final_ref + "_"):
|
||||
final_ref = None
|
||||
|
||||
if final_ref is None:
|
||||
final_ref = step_parser.process_name(object_info.name)[1]
|
||||
|
||||
final_ref = _object_reference_clean_re.search(final_ref).group(1)
|
||||
|
||||
if _bad_reference_re.search(final_ref) is not None:
|
||||
final_ref = "inconnu"
|
||||
if object_info.layer is not None and _bad_reference_re.search(object_info.layer.reference) is None:
|
||||
final_ref = object_info.layer.reference
|
||||
|
||||
if object_info.name == final_ref or object_info.name.startswith(final_ref + "_"):
|
||||
new_label = object_info.layer.reference + "_" + final_ref
|
||||
else:
|
||||
if _bad_reference_re.search(object_info.name) is None or final_ref == "inconnu":
|
||||
new_label = object_info.name
|
||||
else:
|
||||
new_label = final_ref
|
||||
|
||||
if _has_number_re.search(new_label) is None:
|
||||
new_label = new_label + "_01"
|
||||
|
||||
obj.Label = new_label
|
||||
|
||||
if "Base_Reference" not in obj.PropertiesList:
|
||||
obj.addProperty("App::PropertyString", "Base_Reference", "Base")
|
||||
obj.Base_Reference = final_ref
|
||||
|
||||
if "Base_Layer" not in obj.PropertiesList:
|
||||
obj.addProperty("App::PropertyString", "Base_Layer", "Base")
|
||||
if object_info.layer is None:
|
||||
obj.Base_Layer = ""
|
||||
else:
|
||||
obj.Base_Layer = object_info.layer.name
|
||||
|
||||
feature_idx = feature_idx + 1
|
||||
|
||||
print("STEP names import finished")
|
||||
|
||||
def parse_vhelio_reference(self, str):
|
||||
object_reference_match = _object_reference_re.search(str)
|
||||
if object_reference_match is not None:
|
||||
return object_reference_match.group(2)
|
||||
return None
|
||||
|
||||
from ahb_command import AHB_CommandWrapper
|
||||
AHB_CommandWrapper.addGuiCommand('AHB_parse_step', AHB_ParseStep())
|
@ -2,6 +2,8 @@ import FreeCADGui as Gui
|
||||
import FreeCAD as App
|
||||
|
||||
class AHB_View_Annotate:
|
||||
updating_balloon = False
|
||||
|
||||
def GetResources(self):
|
||||
return {"MenuText": "View/Annotate",
|
||||
"ToolTip": "Annotates a TechDraw view with object names",
|
||||
@ -12,31 +14,99 @@ class AHB_View_Annotate:
|
||||
return True
|
||||
|
||||
def Activated(self):
|
||||
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench")
|
||||
workbench.docObserver.onObjectTypeChanged('view_annotate_balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop))
|
||||
workbench.docObserver.onObjectTypeSelected('view_annotate_balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point))
|
||||
|
||||
doc = App.activeDocument()
|
||||
page = doc.getObject('Page')
|
||||
view = page.Views[0]
|
||||
|
||||
if len(Gui.Selection.getSelection()) != 1:
|
||||
raise Exception("Veuillez sélectionner exactement un objet")
|
||||
|
||||
object = Gui.Selection.getSelection()[0]
|
||||
raise Exception("Please select exactly one TechDraw view")
|
||||
|
||||
view = Gui.Selection.getSelection()[0]
|
||||
if view.TypeId != 'TechDraw::DrawViewPart':
|
||||
raise Exception("Selected object is not a TechDraw view")
|
||||
|
||||
page = None
|
||||
for obj in doc.Objects:
|
||||
if obj.TypeId == 'TechDraw::DrawPage':
|
||||
if view in obj.Views:
|
||||
page = obj
|
||||
if page is None:
|
||||
raise Exception("Can't find page in which the selected view is located")
|
||||
|
||||
# Remove balloons referencing missing objects
|
||||
for balloon in page.Views:
|
||||
if balloon.TypeId == 'TechDraw::DrawViewBalloon' and "AssemblyHandbook_PartName" in balloon.PropertiesList:
|
||||
partLink = doc.getObject(balloon.AssemblyHandbook_PartName)
|
||||
if partLink is None or partLink not in view.XSource:
|
||||
print(balloon.Name + " references missing object " + balloon.AssemblyHandbook_PartName + ", removing balloon")
|
||||
doc.removeObject(balloon.Name)
|
||||
|
||||
for partLink in view.XSource:
|
||||
balloon = None
|
||||
|
||||
# Search an existing balloon to update
|
||||
for obj in page.Views:
|
||||
if obj.TypeId == 'TechDraw::DrawViewBalloon' and "AssemblyHandbook_PartName" in obj.PropertiesList and obj.AssemblyHandbook_PartName == partLink.Name:
|
||||
balloon = obj
|
||||
|
||||
# Create a new balloon if needed
|
||||
if balloon is None:
|
||||
balloon = App.ActiveDocument.addObject("TechDraw::DrawViewBalloon", "Balloon")
|
||||
balloon.SourceView = view
|
||||
|
||||
balloon.addProperty("App::PropertyString", "AssemblyHandbook_PartName", "AssemblyHandbook")
|
||||
balloon.AssemblyHandbook_PartName = partLink.Name
|
||||
|
||||
page.addView(balloon)
|
||||
|
||||
self.updateBalloon(balloon)
|
||||
|
||||
balloon.X = int(balloon.OriginX) + 100
|
||||
balloon.Y = int(balloon.OriginY) + 100
|
||||
else:
|
||||
self.updateBalloon(balloon)
|
||||
|
||||
def onBalloonChanged(self, obj, prop):
|
||||
# Avoid reentry
|
||||
if self.updating_balloon:
|
||||
return
|
||||
|
||||
#print('Balloon changed: ' + obj.Name + '.' + prop)
|
||||
if prop == 'Y' and "AssemblyHandbook_PartName" in obj.PropertiesList:
|
||||
self.updating_balloon = True
|
||||
self.updateBalloon(obj)
|
||||
self.updating_balloon = False
|
||||
|
||||
def onBalloonSelected(self, operation, obj, sub, point):
|
||||
#print(operation, obj.Name, sub, point)
|
||||
if "AssemblyHandbook_PartName" in obj.PropertiesList:
|
||||
if operation == 'added':
|
||||
print("Selected balloon of " + obj.AssemblyHandbook_PartName)
|
||||
elif operation == 'removed':
|
||||
print("Deselected balloon of " + obj.AssemblyHandbook_PartName)
|
||||
|
||||
def updateBalloon(self, balloon):
|
||||
doc = App.activeDocument()
|
||||
view = balloon.SourceView
|
||||
partLink = doc.getObject(balloon.AssemblyHandbook_PartName)
|
||||
|
||||
# Get object center in view space
|
||||
objectCenterWorld = object.LinkPlacement.Matrix.multiply(object.LinkedObject.Shape.CenterOfGravity)
|
||||
objectCenterWorld = partLink.LinkPlacement.Matrix.multiply(partLink.LinkedObject.Shape.CenterOfGravity)
|
||||
vertId = view.makeCosmeticVertex3d(objectCenterWorld)
|
||||
vert = view.getCosmeticVertex(vertId)
|
||||
objectCenterView = vert.Point
|
||||
view.removeCosmeticVertex(vertId)
|
||||
|
||||
# Create Balloon
|
||||
balloon = App.ActiveDocument.addObject("TechDraw::DrawViewBalloon", "Balloon")
|
||||
balloon.SourceView = view
|
||||
|
||||
balloon.OriginX = objectCenterView.x
|
||||
balloon.OriginY = objectCenterView.y
|
||||
balloon.Text = "1"
|
||||
balloon.Y = objectCenterView.x + 20
|
||||
balloon.X = objectCenterView.y + 20
|
||||
page.addView(balloon)
|
||||
|
||||
balloon.Text = partLink.LinkedObject.Document.Name
|
||||
balloon.ViewObject.Font = 'DejaVu Sans'
|
||||
balloon.ViewObject.Fontsize = 4
|
||||
balloon.BubbleShape = 'Inspection'
|
||||
balloon.EndTypeScale = 4
|
||||
|
||||
from ahb_command import AHB_CommandWrapper
|
||||
AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate())
|
||||
|
58
ahb_document_observer.py
Normal file
58
ahb_document_observer.py
Normal file
@ -0,0 +1,58 @@
|
||||
import FreeCAD as App
|
||||
import FreeCADGui as Gui
|
||||
|
||||
class DocObserver:
|
||||
changed_object_by_type = {}
|
||||
selection_by_type = {}
|
||||
|
||||
was_selected = []
|
||||
|
||||
def __init__(self):
|
||||
Gui.Selection.addObserver(self)
|
||||
|
||||
def slotChangedObject(self, obj, prop):
|
||||
#print("object changed: " + str(obj).replace('<', '').replace(' object>', '') + " " + obj.Name + " : " + str(prop))
|
||||
callbacks = self.changed_object_by_type.get(obj.TypeId, None)
|
||||
if callbacks is not None:
|
||||
for callback in callbacks.values():
|
||||
callback(obj, prop)
|
||||
|
||||
def addSelection(self, doc, obj_name, sub, pnt):
|
||||
self.was_selected = Gui.Selection.getSelection()
|
||||
obj = App.getDocument(doc).getObject(obj_name)
|
||||
callbacks = self.selection_by_type.get(obj.TypeId, None)
|
||||
if callbacks is not None:
|
||||
for callback in callbacks.values():
|
||||
callback('added', obj, sub, pnt)
|
||||
|
||||
def removeSelection(self, doc, obj_name, sub):
|
||||
self.was_selected = Gui.Selection.getSelection()
|
||||
obj = App.getDocument(doc).getObject(obj_name)
|
||||
callbacks = self.selection_by_type.get(obj.TypeId, None)
|
||||
if callbacks is not None:
|
||||
for callback in callbacks.values():
|
||||
callback('removed', obj, sub, None)
|
||||
|
||||
def clearSelection(self, p):
|
||||
was_selected = self.was_selected
|
||||
self.was_selected = []
|
||||
|
||||
for obj in was_selected:
|
||||
callbacks = self.selection_by_type.get(obj.TypeId, None)
|
||||
if callbacks is not None:
|
||||
for callback in callbacks.values():
|
||||
callback('removed', obj, None, None)
|
||||
|
||||
def onObjectTypeChanged(self, callback_id: str, type_id: str, callback):
|
||||
callbacks = self.changed_object_by_type.get(type_id, None)
|
||||
if callbacks is None:
|
||||
callbacks = {}
|
||||
self.changed_object_by_type[type_id] = callbacks
|
||||
callbacks[callback_id] = callback
|
||||
|
||||
def onObjectTypeSelected(self, callback_id: str, type_id: str, callback):
|
||||
callbacks = self.selection_by_type.get(type_id, None)
|
||||
if callbacks is None:
|
||||
callbacks = {}
|
||||
self.selection_by_type[type_id] = callbacks
|
||||
callbacks[callback_id] = callback
|
Loading…
Reference in New Issue
Block a user