You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
8.7 KiB
200 lines
8.7 KiB
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", |
|
"Pixmap": "" |
|
} |
|
|
|
def IsActive(self): |
|
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() |
|
|
|
if len(Gui.Selection.getSelection()) != 1: |
|
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 = self.getViewPage(view) |
|
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: |
|
if balloon.SourceView != view: continue |
|
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: |
|
if obj.SourceView != view: continue |
|
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) + 20 |
|
balloon.Y = int(balloon.OriginY) + 20 |
|
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, balloon, sub, point): |
|
import time |
|
|
|
#print(operation, obj.Name, sub, point) |
|
if "AssemblyHandbook_PartName" in balloon.PropertiesList: |
|
if operation == 'added': |
|
#print("Selected balloon of " + balloon.AssemblyHandbook_PartName) |
|
|
|
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench |
|
doc = balloon.Document |
|
view = balloon.SourceView |
|
page = self.getViewPage(view) |
|
|
|
partLink = doc.getObject(balloon.AssemblyHandbook_PartName) |
|
|
|
cache_edges_start_time = time.perf_counter() |
|
|
|
part_view = workbench.partsCache.getPart2DView(view, partLink) |
|
|
|
cache_edges_end_time = time.perf_counter() |
|
|
|
update_final_edges_start_time = time.perf_counter() |
|
|
|
# iterate edges of actual view and highlight matching edges |
|
center = self.computePartCenter(view, partLink) |
|
for edgeIdx in range(10000): |
|
hasEdge = False |
|
try: |
|
edge = view.getEdgeByIndex(edgeIdx) |
|
hasEdge = True |
|
except: |
|
pass |
|
if not hasEdge: |
|
break |
|
|
|
# reset edge format |
|
#view.formatGeometricEdge(edgeIdx,1,0.25,0,True) |
|
|
|
if not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1: |
|
edgeData = [ |
|
edge.Vertexes[0].X - center.x, |
|
edge.Vertexes[0].Y - center.y, |
|
edge.Vertexes[1].X - center.x, |
|
edge.Vertexes[1].Y - center.y |
|
] |
|
v0 = App.Vector(edgeData[0], edgeData[1]) |
|
v1 = App.Vector(edgeData[2], edgeData[3]) |
|
|
|
for line in part_view.cached_lines: |
|
l0 = App.Vector(line[0], line[1]) |
|
l1 = App.Vector(line[2], line[3]) |
|
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3]) |
|
d = v0.distanceToLineSegment(l0, l1).Length + v1.distanceToLineSegment(l0, l1).Length |
|
if d < 0.001: |
|
view.formatGeometricEdge(edgeIdx,1,0.25,(0,0.85,0),True) |
|
|
|
view.requestPaint() |
|
|
|
update_final_edges_end_time = time.perf_counter() |
|
|
|
#print("Part render time: " + str(round((cache_edges_end_time - cache_edges_start_time) * 1000) / 1000) + "s") |
|
#print("Update view time: " + str(round((update_final_edges_end_time - update_final_edges_start_time) * 1000) / 1000) + "s") |
|
|
|
elif operation == 'removed': |
|
#print("Deselected balloon of " + balloon.AssemblyHandbook_PartName) |
|
|
|
view = balloon.SourceView |
|
|
|
# reset edges format |
|
for edgeIdx in range(10000): |
|
hasEdge = False |
|
try: |
|
edge = view.getEdgeByIndex(edgeIdx) |
|
hasEdge = True |
|
except: |
|
pass |
|
if not hasEdge: |
|
break |
|
|
|
view.formatGeometricEdge(edgeIdx,1,0.25,0,True) |
|
|
|
view.requestPaint() |
|
|
|
def updateBalloon(self, balloon): |
|
doc = App.activeDocument() |
|
view = balloon.SourceView |
|
obj = doc.getObject(balloon.AssemblyHandbook_PartName) |
|
|
|
objectCenterView = self.computePartCenter(view, obj) |
|
|
|
balloon.OriginX = objectCenterView.x |
|
balloon.OriginY = objectCenterView.y |
|
|
|
balloon.Text = obj.LinkedObject.Document.Name if obj.TypeId == 'App::Link' else obj.Name |
|
balloon.ViewObject.Font = 'DejaVu Sans' |
|
balloon.ViewObject.Fontsize = 4 |
|
balloon.BubbleShape = 'Inspection' |
|
balloon.EndTypeScale = 4 |
|
|
|
def getViewPage(self, view): |
|
for obj in view.InList: |
|
if obj.TypeId == 'TechDraw::DrawPage': |
|
if view in obj.Views: |
|
return obj |
|
return None |
|
|
|
def computePartCenter(self, view, obj): |
|
if obj.TypeId == 'App::Link': |
|
partLink = obj |
|
objectCenterWorld = partLink.LinkPlacement.Matrix.multiply(partLink.LinkedObject.Shape.CenterOfGravity) |
|
else: |
|
objectCenterWorld = obj.Shape.CenterOfGravity |
|
|
|
vertId = view.makeCosmeticVertex3d(objectCenterWorld) |
|
vert = view.getCosmeticVertex(vertId) |
|
objectCenterView = vert.Point |
|
view.removeCosmeticVertex(vertId) |
|
return objectCenterView |
|
|
|
from ahb_command import AHB_CommandWrapper |
|
AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate())
|
|
|