Compare commits
No commits in common. "master" and "dev/better-annotations" have entirely different histories.
master
...
dev/better
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,4 +3,3 @@
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
/pydev/
|
||||
|
@ -103,9 +103,6 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
|
||||
|
||||
self.importModule('ahb_cmd_view_refresh')
|
||||
toolbox.append("AHB_view_refresh")
|
||||
|
||||
self.importModule('ahb_cmd_export_parts_list')
|
||||
toolbox.append("AHB_export_parts_list")
|
||||
|
||||
if self.dev:
|
||||
self.importModule('ahb_cmd_reload')
|
||||
|
@ -1,109 +0,0 @@
|
||||
import FreeCADGui as Gui
|
||||
import FreeCAD as App
|
||||
|
||||
import ahb_utils
|
||||
from ahb_material import Material
|
||||
|
||||
class PartInfo:
|
||||
def __init__(self, workbench, document, obj):
|
||||
self.document = document
|
||||
self.reference = obj.Label
|
||||
|
||||
if len(self.reference) == 3 and self.reference[0:1] in ['L', 'M', 'T', 'R', 'E']:
|
||||
self.reference = 'TB_' + self.reference
|
||||
|
||||
workbench.techDrawExtensions.initPartMetadata(obj)
|
||||
|
||||
self.material = 'Unknown'
|
||||
try:
|
||||
self.material = obj.Assembly_handbook_Material
|
||||
except:
|
||||
pass
|
||||
|
||||
self.size = [obj.Shape.BoundBox.XLength, obj.Shape.BoundBox.YLength, obj.Shape.BoundBox.ZLength] # in mm
|
||||
self.volume = obj.Shape.Volume # in mm3
|
||||
self.mass = -1 # in g (negative means unknown mass)
|
||||
density = -1 # in g/cm3
|
||||
material = Material.Get(self.material)
|
||||
if material is not None:
|
||||
density = material.density
|
||||
if density >= 0: self.mass = density * (self.volume / 1000)
|
||||
try:
|
||||
part_mass = obj.Assembly_handbook_Weight
|
||||
if part_mass >= 0:
|
||||
self.mass = part_mass
|
||||
except:
|
||||
pass
|
||||
self.count = 0
|
||||
|
||||
class AHB_ExportPartsList:
|
||||
def GetResources(self):
|
||||
return {"MenuText": "Export parts list (CSV)",
|
||||
"ToolTip": "Exports all parts of the selected assembly in CSV format",
|
||||
"Pixmap": ""
|
||||
}
|
||||
|
||||
def IsActive(self):
|
||||
obj = Gui.Selection.getSelection()
|
||||
if len(obj) == 1:
|
||||
obj = obj[0]
|
||||
return obj.TypeId == 'App::Part' and 'AssemblyType' in obj.PropertiesList
|
||||
return False
|
||||
|
||||
def Activated(self):
|
||||
all_parts = {}
|
||||
|
||||
rootAssembly = Gui.Selection.getSelection()[0]
|
||||
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
|
||||
|
||||
def add_part(part):
|
||||
#print(part.Label + " (" + part.LinkedObject.Label + ")")
|
||||
try:
|
||||
info = all_parts[part.LinkedObject]
|
||||
except:
|
||||
info = PartInfo(workbench, part.LinkedObject.Document.Name, part.LinkedObject)
|
||||
all_parts[part.LinkedObject] = info
|
||||
|
||||
info.count += 1
|
||||
|
||||
def process_group(group):
|
||||
for part in group.Group:
|
||||
if not part.Visibility:
|
||||
continue
|
||||
|
||||
isPartLink = part.TypeId == 'App::Link' or (part.TypeId == 'Part::FeaturePython' and hasattr(part, 'LinkedObject'))
|
||||
partType = None
|
||||
isSinglePart = None
|
||||
try:
|
||||
partType = part.Type
|
||||
isSinglePart = part.Assembly_handbook_IsSinglePart
|
||||
except:
|
||||
pass
|
||||
|
||||
if isPartLink:
|
||||
if isSinglePart != True and (partType == 'Assembly' or (part.Group[1].Label == 'Constraints' and part.Group[2].Label == 'Variables' and part.Group[3].Label == 'Configurations')):
|
||||
process_group(part.LinkedObject)
|
||||
else:
|
||||
add_part(part)
|
||||
else:
|
||||
if part.TypeId != 'App::DocumentObjectGroup' and part.TypeId != 'Spreadsheet::Sheet' and partType != 'App::PropertyContainer':
|
||||
print("??? " + part.Label)
|
||||
pass
|
||||
|
||||
print("Exporting all parts contained in assembly: " + rootAssembly.Label + "...")
|
||||
process_group(rootAssembly)
|
||||
|
||||
file_name = rootAssembly.Document.FileName.replace('.FCStd', '') + '_list.csv'
|
||||
with open(file_name, "w") as f:
|
||||
f.write("Document, Reference, Material, SizeX (mm), SizeY (mm), SizeZ (mm), Volume (cm3), Mass (g), Count\n")
|
||||
for part in all_parts.values():
|
||||
mass_str = ''
|
||||
if part.mass >= 10:
|
||||
mass_str = str(int(part.mass + 0.5))
|
||||
elif part.mass >= 0:
|
||||
mass_str = str(int(part.mass*10 + 0.5) / 10.0)
|
||||
f.write(part.document + "," + part.reference + "," + part.material + "," + str(int(part.size[0]+0.5)) + "," + str(int(part.size[1]+0.5)) + "," + str(int(part.size[2]+0.5)) + "," + str(int(part.volume/100+0.5)/10.0) + "," + mass_str + ", " + str(part.count) + "\n")
|
||||
print("Part list exported to " + file_name)
|
||||
|
||||
from ahb_command import AHB_CommandWrapper
|
||||
AHB_CommandWrapper.addGuiCommand('AHB_export_parts_list', AHB_ExportPartsList())
|
@ -10,7 +10,7 @@ class AHB_New_Step:
|
||||
}
|
||||
|
||||
def IsActive(self):
|
||||
return True
|
||||
return ahb_utils.getCurrentView() is not None
|
||||
|
||||
def Activated(self):
|
||||
import re
|
||||
@ -74,7 +74,6 @@ class AHB_New_Step:
|
||||
view.CoarseView = True
|
||||
view.addProperty("App::PropertyString", "Assembly_handbook_PreviousStepView", "Assembly_handbook")
|
||||
view.addProperty("App::PropertyBool", "Assembly_handbook_RasterView", "Assembly_handbook")
|
||||
view.addProperty("App::PropertyXLinkList", "Assembly_handbook_HideParts", "Assembly_handbook")
|
||||
view.Assembly_handbook_RasterView = raster_view
|
||||
if raster_view:
|
||||
view.Visibility = False
|
||||
|
@ -69,14 +69,12 @@ class AHB_View_Annotate:
|
||||
print(balloon.Name + " references missing object " + ref_name + ", removing balloon")
|
||||
doc.removeObject(balloon.Name)
|
||||
|
||||
balloonsCreated = []
|
||||
for partLink in view.XSource:
|
||||
balloonsCreated.extend(workbench.techDrawExtensions.add_or_update_balloon(view, partLink, ''))
|
||||
|
||||
if len(balloonsCreated) > 0:
|
||||
regroupedBalloons = self.RegroupNearestSimilarBalloons(balloonsCreated)
|
||||
#self.PlaceBalloonsInCircle(regroupedBalloons)
|
||||
self.PlaceBalloonsInCircle(balloonsCreated)
|
||||
balloonsCreated = workbench.techDrawExtensions.add_or_update_balloon(view, partLink, '')
|
||||
if len(balloonsCreated) > 0:
|
||||
regroupedBalloons = self.RegroupNearestSimilarBalloons(balloonsCreated)
|
||||
self.PlaceBalloonsInCircle(regroupedBalloons)
|
||||
#self.PlaceBalloonsInCircle(balloonsCreated)
|
||||
|
||||
workbench.techDrawExtensions.refreshOverlays(page)
|
||||
|
||||
@ -88,13 +86,13 @@ class AHB_View_Annotate:
|
||||
realBalloon = balloon[0] if type(balloon) is list else balloon
|
||||
totalX = totalX + int(realBalloon.OriginX)
|
||||
totalY = totalY + int(realBalloon.OriginY)
|
||||
return App.Vector(totalX / len(balloons), totalY / len(balloons))
|
||||
return App.Base.Vector2d(totalX / len(balloons), totalY / len(balloons))
|
||||
|
||||
def IsSimilarBalloonNear(self, balloonA, balloonB):
|
||||
MAX_DISTANCE_BETWEEN_REGROUPED_BALLOONS = 50
|
||||
if balloonA.Text == balloonB.Text:
|
||||
pos = App.Vector(balloonA.OriginX, balloonA.OriginY)
|
||||
dist = pos.distanceToPoint(App.Vector(balloonB.OriginX, balloonB.OriginY))
|
||||
pos = App.Base.Vector2d(balloonA.OriginX, balloonA.OriginY)
|
||||
dist = pos.distance(App.Base.Vector2d(balloonB.OriginX, balloonB.OriginY))
|
||||
return dist < MAX_DISTANCE_BETWEEN_REGROUPED_BALLOONS
|
||||
else:
|
||||
return False
|
||||
@ -121,13 +119,13 @@ class AHB_View_Annotate:
|
||||
for i in range(nbBalloons):
|
||||
xPos = round(center.x + 600 * math.cos(balloonPosStep * i))
|
||||
yPos = round(center.y + 600 * math.sin(balloonPosStep * i))
|
||||
balloonPos = App.Vector(xPos, yPos)
|
||||
balloonPos = App.Base.Vector2d(xPos, yPos)
|
||||
# Find nearest arrow to avoid arrow crossing each other
|
||||
smallestDistance = 0
|
||||
balloonToUse = None
|
||||
for balloon in balloons:
|
||||
realBalloon = balloon[0] if type(balloon) is list else balloon
|
||||
dist = balloonPos.distanceToPoint(App.Vector(realBalloon.OriginX, realBalloon.OriginY))
|
||||
dist = balloonPos.distance(App.Base.Vector2d(realBalloon.OriginX, realBalloon.OriginY))
|
||||
if smallestDistance == 0 or dist < smallestDistance:
|
||||
smallestDistance = dist
|
||||
balloonToUse = balloon
|
||||
|
@ -4,7 +4,7 @@ import FreeCAD as App
|
||||
class AHB_RefreshView:
|
||||
def GetResources(self):
|
||||
return {"MenuText": "Refresh page (final quality)",
|
||||
"ToolTip": "Redraws the current page, or if one or more pages are selected, redraw all the selected pages",
|
||||
"ToolTip": "Redraws the current page",
|
||||
"Pixmap": ""
|
||||
}
|
||||
|
||||
@ -14,20 +14,9 @@ class AHB_RefreshView:
|
||||
|
||||
def Activated(self):
|
||||
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
|
||||
|
||||
selection = Gui.Selection.getSelection()
|
||||
has_selected_pages = len(selection) > 0
|
||||
for s in selection:
|
||||
if s.TypeId != 'TechDraw::DrawPage':
|
||||
has_selected_pages = False
|
||||
|
||||
if has_selected_pages:
|
||||
for page in selection:
|
||||
workbench.techDrawExtensions.forceRedrawPage(page, fast_render = False)
|
||||
else:
|
||||
page = workbench.techDrawExtensions.getActivePage()
|
||||
if page is not None:
|
||||
workbench.techDrawExtensions.forceRedrawPage(page, fast_render = False)
|
||||
page = workbench.techDrawExtensions.getActivePage()
|
||||
if page is not None:
|
||||
workbench.techDrawExtensions.forceRedrawPage(page, fast_render = False)
|
||||
|
||||
from ahb_command import AHB_CommandWrapper
|
||||
AHB_CommandWrapper.addGuiCommand('AHB_view_refresh', AHB_RefreshView())
|
||||
|
@ -43,28 +43,11 @@ class DocLinkObserver:
|
||||
class DocObserver:
|
||||
changed_object_by_type = {}
|
||||
selection_by_type = {}
|
||||
doc_callbacks = {}
|
||||
|
||||
was_selected = []
|
||||
|
||||
def __init__(self):
|
||||
Gui.Selection.addObserver(self)
|
||||
|
||||
def slotActivateDocument(self, doc):
|
||||
#print('slotActivateDocument', doc.Name)
|
||||
self._triggerDocumentEvent(doc, 'activate')
|
||||
|
||||
def slotCreatedDocument(self, doc):
|
||||
#print('slotCreatedDocument', doc.Name)
|
||||
self._triggerDocumentEvent(doc, 'created')
|
||||
|
||||
def slotDeletedDocument(self, doc):
|
||||
#print('slotDeletedDocument', doc.Name)
|
||||
self._triggerDocumentEvent(doc, 'deleted')
|
||||
|
||||
def _triggerDocumentEvent(self, doc, event):
|
||||
for callback in self.doc_callbacks.values():
|
||||
callback(doc, event)
|
||||
|
||||
def slotChangedObject(self, obj, prop):
|
||||
#print("object changed: " + str(obj).replace('<', '').replace(' object>', '') + " " + obj.Name + " : " + str(prop))
|
||||
@ -111,7 +94,4 @@ class DocObserver:
|
||||
if callbacks is None:
|
||||
callbacks = {}
|
||||
self.selection_by_type[type_id] = callbacks
|
||||
callbacks[callback_id] = callback
|
||||
|
||||
def onDocumentEvent(self, callback_id: str, callback):
|
||||
self.doc_callbacks[callback_id] = callback
|
||||
callbacks[callback_id] = callback
|
@ -1,24 +0,0 @@
|
||||
class Material:
|
||||
def __init__(self, ID, density):
|
||||
self.ID = ID
|
||||
self.density = density
|
||||
|
||||
DB = []
|
||||
|
||||
@staticmethod
|
||||
def GetMaterialIDs():
|
||||
result = []
|
||||
for m in Material.DB:
|
||||
result.append(m.ID)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def Get(ID):
|
||||
for m in Material.DB:
|
||||
if m.ID == ID: return m
|
||||
return None
|
||||
|
||||
Material.DB.append(Material('Stainless steel', density = 8.00))
|
||||
Material.DB.append(Material('Aluminium', density = 2.71))
|
||||
Material.DB.append(Material('Wood (pine)', density = 0.55))
|
||||
Material.DB.append(Material('Plywood', density = 0.6))
|
@ -1,13 +1,5 @@
|
||||
import FreeCAD as App
|
||||
import FreeCADGui as Gui
|
||||
from datetime import datetime
|
||||
|
||||
def print_verbose(msg):
|
||||
verbose = False
|
||||
if verbose:
|
||||
now = datetime.now()
|
||||
current_time = now.strftime("%H:%M:%S")
|
||||
print(current_time, msg)
|
||||
|
||||
class RasterView:
|
||||
def __init__(self, view):
|
||||
@ -97,7 +89,7 @@ class RasterView:
|
||||
result.extend(self._flatten_objects_tree([obj.LinkedObject]))
|
||||
elif obj.TypeId in ['App::Part', 'App::DocumentObjectGroup']:
|
||||
result.extend(self._flatten_objects_tree(obj.Group))
|
||||
elif self._should_render(obj) or self._should_render_as_is(obj) or obj.TypeId in ['PartDesign::CoordinateSystem', 'PartDesign::Line']:
|
||||
elif obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'PartDesign::CoordinateSystem', 'PartDesign::Line', 'Part::Mirroring', 'Part::Cut', 'Part::Part2DObjectPython']:
|
||||
result.append(obj)
|
||||
if hasattr(obj, 'Group'):
|
||||
result.extend(self._flatten_objects_tree(obj.Group))
|
||||
@ -105,10 +97,7 @@ class RasterView:
|
||||
return result
|
||||
|
||||
def _should_render(self, obj):
|
||||
return obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'Part::Mirroring', 'Part::Cut', 'Part::Part2DObjectPython', 'Part::MultiFuse', 'Part::Loft', 'Part::Torus', 'Part::Cylinder']
|
||||
|
||||
def _should_render_as_is(self, obj):
|
||||
return obj.TypeId in ['App::FeaturePython']
|
||||
return obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'Part::Mirroring', 'Part::Cut', 'Part::Part2DObjectPython']
|
||||
|
||||
def render(self, fast_render = True):
|
||||
from pivy import coin
|
||||
@ -124,45 +113,21 @@ class RasterView:
|
||||
|
||||
self.init_image()
|
||||
|
||||
print_verbose('Rasterizing ' + view.Label + " to " + self.image_file_name + "...")
|
||||
print('Rasterizing ' + view.Label + " to " + self.image_file_name + "...")
|
||||
|
||||
dir = os.path.dirname(self.image_file_name)
|
||||
if not os.path.exists(dir):
|
||||
os.makedirs(dir)
|
||||
|
||||
if 'Assembly_handbook_RasterSavedView' in view.PropertiesList and view.Assembly_handbook_RasterSavedView is not None:
|
||||
tmp_doc = view.Assembly_handbook_RasterSavedView.Document
|
||||
close_tmp_doc = False
|
||||
else:
|
||||
tmp_doc = App.newDocument('tmp_raster', hidden=False, temp=False)
|
||||
close_tmp_doc = True
|
||||
|
||||
transparent_background = True
|
||||
if 'Assembly_handbook_TransparentBackground' in view.PropertiesList:
|
||||
transparent_background = view.Assembly_handbook_TransparentBackground
|
||||
tmp_doc = App.newDocument('tmp_raster', hidden=False, temp=False)
|
||||
|
||||
objects_to_reset = {}
|
||||
duplicated_parts = {}
|
||||
try:
|
||||
print_verbose("Preparing scene...")
|
||||
|
||||
# Clean existing scene (if any)
|
||||
sceneGroup = tmp_doc.getObject('Scene')
|
||||
if sceneGroup is not None:
|
||||
sceneGroup.removeObjectsFromDocument()
|
||||
tmp_doc.removeObject(sceneGroup.Name)
|
||||
|
||||
# construct new scene with links to the parts we want
|
||||
sceneGroup = tmp_doc.addObject('App::DocumentObjectGroup', 'Scene')
|
||||
prev_parts = []
|
||||
new_parts = []
|
||||
all_parts = view.XSource + view.Source
|
||||
|
||||
objects_to_hide = []
|
||||
if 'Assembly_handbook_HideParts' in view.PropertiesList:
|
||||
objects_to_hide = self._flatten_objects_tree(view.Assembly_handbook_HideParts)
|
||||
|
||||
for part in all_parts:
|
||||
for part in view.XSource:
|
||||
link = tmp_doc.addObject('App::Link', part.Name)
|
||||
link.Label = part.Label
|
||||
if part.TypeId == 'App::Link':
|
||||
@ -173,14 +138,10 @@ class RasterView:
|
||||
link.Placement = part.Placement
|
||||
else:
|
||||
link.LinkedObject = part
|
||||
if part.TypeId in ['Part::Part2DObjectPython']:
|
||||
link.Placement = part.Placement
|
||||
|
||||
is_new_part = workbench.techDrawExtensions.isNewPartInView(view, part)
|
||||
|
||||
if not fast_render:
|
||||
# check if another part with different render settings will conflict with ours
|
||||
# a conflict occurs when two parts link to the same object (directly or indirectly), because render settings (such as color) are set at the object level
|
||||
is_conflicting = False
|
||||
if link.LinkedObject in duplicated_parts.keys():
|
||||
link.LinkedObject = duplicated_parts[link.LinkedObject]
|
||||
@ -189,7 +150,7 @@ class RasterView:
|
||||
for other_part in other_parts:
|
||||
other_objects = self._flatten_objects_tree([other_part])
|
||||
for obj in self._flatten_objects_tree([link]):
|
||||
if self._should_render(obj) and obj in other_objects:
|
||||
if obj in other_objects:
|
||||
is_conflicting = True
|
||||
|
||||
if is_conflicting:
|
||||
@ -201,21 +162,18 @@ class RasterView:
|
||||
part_copy.Label = part.Label
|
||||
duplicated_parts[link.LinkedObject] = part_copy
|
||||
link.LinkedObject = part_copy
|
||||
part_copy.ViewObject.Visibility = False
|
||||
|
||||
sceneGroup.addObject(link)
|
||||
|
||||
if is_new_part:
|
||||
new_parts.append(link)
|
||||
else:
|
||||
prev_parts.append(link)
|
||||
|
||||
# hide objects that we don't want to display ; also make a backup of properties we want to reset after we're done
|
||||
# hide objects that we don't want to display ; also make a backup of properties we want to reset after we're done
|
||||
for obj in self._flatten_objects_tree([link]):
|
||||
if obj in objects_to_reset.keys():
|
||||
continue
|
||||
|
||||
if self._should_render(obj) and not obj in objects_to_hide:
|
||||
if self._should_render(obj):
|
||||
if not fast_render:
|
||||
objects_to_reset[obj] = (
|
||||
obj.ViewObject.Visibility,
|
||||
@ -227,12 +185,6 @@ class RasterView:
|
||||
obj.ViewObject.LineWidth,
|
||||
obj.ViewObject.DisplayMode
|
||||
)
|
||||
|
||||
if not obj.ViewObject.Visibility:
|
||||
obj.ViewObject.ShapeMaterial.AmbientColor = (0, 0, 0)
|
||||
obj.ViewObject.ShapeMaterial.DiffuseColor = (0, 0, 0)
|
||||
obj.ViewObject.ShapeMaterial.SpecularColor = (0, 0, 0)
|
||||
obj.ViewObject.ShapeMaterial.EmissiveColor = (0, 0, 0)
|
||||
else:
|
||||
objects_to_reset[obj] = (
|
||||
obj.ViewObject.Visibility,
|
||||
@ -248,32 +200,14 @@ class RasterView:
|
||||
rot *= coin.SbRotation(coin.SbVec3f(0,0,1), coin.SbVec3f(view.Direction.x,view.Direction.y,view.Direction.z))
|
||||
cam.orientation.setValue(rot)
|
||||
|
||||
targetViewVolume = None
|
||||
try:
|
||||
targetViewVolume = view.Assembly_handbook_ViewVolume
|
||||
except:
|
||||
pass
|
||||
|
||||
if targetViewVolume is None:
|
||||
tmp_doc_view.fitAll()
|
||||
else:
|
||||
sceneGroup.ViewObject.Visibility = False
|
||||
viewVolumeLink = tmp_doc.addObject('App::Link', 'ViewVolume')
|
||||
viewVolumeLink.LinkedObject = targetViewVolume
|
||||
viewVolumeLink.Placement = targetViewVolume.Placement
|
||||
tmp_doc_view.fitAll()
|
||||
tmp_doc.removeObject(viewVolumeLink.Name)
|
||||
sceneGroup.ViewObject.Visibility = True
|
||||
print_verbose("Near=" + str(cam.nearDistance.getValue()) + ", far="+str(cam.farDistance.getValue()))
|
||||
cam.nearDistance.setValue(cam.nearDistance.getValue() - 1000)
|
||||
cam.farDistance.setValue(cam.farDistance.getValue() + 1000)
|
||||
tmp_doc_view.fitAll()
|
||||
|
||||
viewVolume = cam.getViewVolume(0.0)
|
||||
self.image_view.Assembly_handbook_ViewVolumeWidth = viewVolume.getWidth()
|
||||
self.image_view.Assembly_handbook_ViewVolumeHeight = viewVolume.getHeight()
|
||||
self.image_view.Assembly_handbook_ViewVolumeDepth = viewVolume.getDepth()
|
||||
|
||||
max_res = 3200
|
||||
max_res = 3200 # todo: keep aspect ratio when we limit max image dimensions
|
||||
#max_res = 1500
|
||||
resolution = [
|
||||
int(viewVolume.getWidth() * view.Scale * 10),
|
||||
@ -287,46 +221,23 @@ class RasterView:
|
||||
resolution[1] = int(max_res)
|
||||
|
||||
if fast_render:
|
||||
print_verbose("Fast rasterization...")
|
||||
composite_img = self._render_lines(tmp_doc, resolution, prev_parts + new_parts, (0.0, 0.0, 0.0), [])
|
||||
else:
|
||||
# render old parts in gray lines
|
||||
print_verbose("Rendering old parts (gray)...")
|
||||
prev_parts_img = self._render_lines(tmp_doc, resolution, prev_parts, (0.6, 0.6, 0.6), [], fast_render)
|
||||
|
||||
# render new parts in black lines (old parts can mask them)
|
||||
print_verbose("Rendering new parts (black)...")
|
||||
new_parts_img = self._render_lines(tmp_doc, resolution, new_parts, (0.0, 0.0, 0.0), prev_parts, fast_render)
|
||||
|
||||
# create the composite image
|
||||
print_verbose("Compositing images...")
|
||||
composite_img = prev_parts_img.copy()
|
||||
composite_img.paste(new_parts_img, None, new_parts_img)
|
||||
|
||||
# Optimize the image to reduce storage size
|
||||
if not fast_render:
|
||||
print_verbose("Optimizing PNG size...")
|
||||
num_colors = 32
|
||||
|
||||
# All-or-nothing alpha: we use a white background and only make pixels fully transparent where alpha is zero, to not loose antialiasing
|
||||
bg_img = Image.new(composite_img.mode, composite_img.size, color = '#ffffff')
|
||||
bg_img.paste(composite_img.convert('RGB'), composite_img)
|
||||
final_alpha = composite_img.split()[3].point(lambda p: 0 if p <= int(255/num_colors+0.5) else 255)
|
||||
if not transparent_background:
|
||||
final_alpha = final_alpha.point(lambda p: 255)
|
||||
composite_img = bg_img
|
||||
composite_img.putalpha(final_alpha)
|
||||
|
||||
# Convert to indexed colors
|
||||
composite_img = composite_img.quantize(colors=num_colors, dither=Image.Dither.NONE)
|
||||
|
||||
finally:
|
||||
print_verbose("Cleaning scene...")
|
||||
#raise Exception("test")
|
||||
# restore properties on objects we have modified
|
||||
for obj, props in objects_to_reset.items():
|
||||
obj.ViewObject.Visibility = props[0]
|
||||
if self._should_render(obj) and not obj in objects_to_hide:
|
||||
if self._should_render(obj):
|
||||
obj.ViewObject.LineColor = props[1]
|
||||
obj.ViewObject.ShapeMaterial.AmbientColor = props[2]
|
||||
obj.ViewObject.ShapeMaterial.DiffuseColor = props[3]
|
||||
@ -334,18 +245,14 @@ class RasterView:
|
||||
obj.ViewObject.ShapeMaterial.EmissiveColor = props[5]
|
||||
obj.ViewObject.LineWidth = props[6]
|
||||
obj.ViewObject.DisplayMode = props[7]
|
||||
obj.ViewObject.PointMaterial.Transparency = 0
|
||||
|
||||
# remove the temporary document
|
||||
if close_tmp_doc:
|
||||
App.closeDocument(tmp_doc.Name)
|
||||
|
||||
print_verbose("Finalizing view...")
|
||||
App.closeDocument(tmp_doc.Name)
|
||||
|
||||
# Crop the image, which is also used to deduce the center of the source view
|
||||
original_size = composite_img.size
|
||||
|
||||
diff_source_img = composite_img.split()[-1]
|
||||
diff_source_img = composite_img.split()[3]
|
||||
bg = Image.new(diff_source_img.mode, diff_source_img.size, '#000000') # fills an image with the background color
|
||||
diff = ImageChops.difference(diff_source_img, bg) # diff between the actual image and the background color
|
||||
bbox = diff.getbbox() # finds border size (non-black portion of the image)
|
||||
@ -384,13 +291,11 @@ class RasterView:
|
||||
image.Scale = image_scale
|
||||
image.X = view.X
|
||||
image.Y = view.Y
|
||||
image.ImageFile = self.image_file_name
|
||||
image.ImageFile = self.image_file_name # TODO: see if it's possible to set a relative path
|
||||
image.ViewObject.Crop = True
|
||||
image.Width = composite_img.size[0] * image_scale / 10.0 * 1.01
|
||||
image.Height = composite_img.size[1] * image_scale / 10.0 * 1.01
|
||||
image.recompute()
|
||||
|
||||
print_verbose("Done")
|
||||
|
||||
def _render_lines(self, doc, resolution, parts, line_color, masking_parts, fast_render = True):
|
||||
import tempfile
|
||||
@ -398,66 +303,48 @@ class RasterView:
|
||||
|
||||
doc_view = Gui.getDocument(doc.Name).mdiViewsOfType('Gui::View3DInventor')[0]
|
||||
|
||||
# render lines in blue, background in red, fill shapes in green
|
||||
# the green band contains the lines image, the red band contains the inverted alpha layer
|
||||
# if there is a clipping plane set with "Fill clip plane", the blue band contains the intersection with the clip plane
|
||||
configured = []
|
||||
print_verbose('Preparing objects for line rendering...')
|
||||
# render lines in black, background in red, fill shapes in green
|
||||
# the green band contains the lines images, the red band contains the inverted alpha layer
|
||||
for link in doc.findObjects():
|
||||
if link in parts or link in masking_parts:
|
||||
link.ViewObject.Visibility = True
|
||||
|
||||
# in current version of freecad, link override material does not allow to override all material properties, for example emissive color, so we have to change material of the linked object
|
||||
for obj in self._flatten_objects_tree([link]):
|
||||
if obj in configured: continue
|
||||
configured.append(obj)
|
||||
|
||||
if self._should_render(obj) and not fast_render:
|
||||
obj.ViewObject.DisplayMode = 'Flat Lines'
|
||||
obj.ViewObject.PointMaterial.Transparency = 1.0 # hide points
|
||||
obj.ViewObject.LineColor = (0.0, 0.0, 1.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
|
||||
obj.ViewObject.LineColor = (0.0, 0.0, 0.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
|
||||
obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0)
|
||||
obj.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0)
|
||||
obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0)
|
||||
obj.ViewObject.ShapeMaterial.EmissiveColor = (0.0, 1.0, 1.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
|
||||
obj.ViewObject.ShapeMaterial.EmissiveColor = (0.0, 1.0, 0.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
|
||||
|
||||
# We need to set two different values otherwise freecad does not always update LineWidth of sub-elements
|
||||
obj.ViewObject.LineWidth = 1.0
|
||||
obj.ViewObject.LineWidth = 3.0
|
||||
obj.ViewObject.LineWidth = 2.0
|
||||
else:
|
||||
link.ViewObject.Visibility = False
|
||||
|
||||
print_verbose('Rendering lines...')
|
||||
temp_file_name = tempfile.gettempdir() + "/ahb_temp_image.png"
|
||||
#temp_file_name = "/home/youen/tmp/ahb_temp_image.png"
|
||||
doc_view.saveImage(temp_file_name, resolution[0]+2, resolution[1]+2, "#ff00ff") # we add 1 pixel border that we will need to crop later
|
||||
doc_view.saveImage(temp_file_name, resolution[0]+2, resolution[1]+2, "#ff0000") # we add 1 pixel border that we will need to crop later
|
||||
lines_bands_img = self._read_image(temp_file_name)
|
||||
|
||||
lines_bands = lines_bands_img.split()
|
||||
lines_img = lines_bands[1]
|
||||
alpha_img = lines_bands[0].point(lambda p: 255 - p)
|
||||
clip_img = lines_bands[2]
|
||||
|
||||
generate_outlines = not fast_render
|
||||
#generate_outlines = False
|
||||
if generate_outlines:
|
||||
# Render all shapes with different colors, in order to extract outlines (where color changes)
|
||||
# This is needed because FreeCAD does not render lines on the boundary of curve shapes, such as spheres or cylinders
|
||||
# The technique could be improved by using the depth buffer instead, in order to detect boundaries within the same object
|
||||
print_verbose('Preparing objects for outline rendering...')
|
||||
step = 8
|
||||
r = step
|
||||
g = step
|
||||
b = step
|
||||
configured = []
|
||||
for link in doc.findObjects():
|
||||
if link in parts or link in masking_parts:
|
||||
for obj in self._flatten_objects_tree([link]):
|
||||
if obj in configured: continue
|
||||
configured.append(obj)
|
||||
|
||||
if self._should_render(obj) and obj.TypeId != 'Part::Part2DObjectPython':
|
||||
configured.append(obj)
|
||||
obj.ViewObject.DisplayMode = 'Shaded'
|
||||
obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0)
|
||||
obj.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0)
|
||||
@ -476,19 +363,16 @@ class RasterView:
|
||||
else:
|
||||
obj.ViewObject.Visibility = False
|
||||
|
||||
print_verbose('Rendering shapes...')
|
||||
doc_view.saveImage(temp_file_name, (resolution[0]+2)*2, (resolution[1]+2)*2, "#ffffff") # shapes are rendered at twice the resolution for antialiasing
|
||||
shapes_img = self._read_image(temp_file_name)
|
||||
|
||||
print_verbose('Extracting outlines...')
|
||||
outlines_img = None
|
||||
for x in range(0, 5):
|
||||
for y in range(0, 5):
|
||||
if x == 2 and y == 2: continue
|
||||
if (x-2)*(x-2) + (y-2)+(y-2) > 4: continue
|
||||
kernel = [0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0]
|
||||
kernel[y * 5 + x] = -1
|
||||
partial_outlines = shapes_img.filter(ImageFilter.Kernel((5, 5), kernel, 1, 127))
|
||||
for x in range(0, 3):
|
||||
for y in range(0, 3):
|
||||
if x == 1 and y == 1: continue
|
||||
kernel = [0, 0, 0, 0, 1, 0, 0, 0, 0]
|
||||
kernel[y * 3 + x] = -1
|
||||
partial_outlines = shapes_img.filter(ImageFilter.Kernel((3, 3), kernel, 1, 127))
|
||||
partial_outlines = partial_outlines.point(lambda p: 255 if p == 127 else 0)
|
||||
partial_outlines = partial_outlines.convert("L")
|
||||
partial_outlines = partial_outlines.point(lambda p: 255 if p == 255 else 0)
|
||||
@ -497,7 +381,6 @@ class RasterView:
|
||||
else:
|
||||
outlines_img.paste(partial_outlines, None, partial_outlines.point(lambda p: 0 if p == 255 else 255))
|
||||
|
||||
print_verbose('Combining lines and outlines...')
|
||||
lines_fullres = lines_img.resize(outlines_img.size, Image.NEAREST)
|
||||
lines_fullres.paste(outlines_img, None, outlines_img.point(lambda p: 255 if p == 0 else 0))
|
||||
#lines_fullres.paste(255, alpha_fullres.point(lambda p: 255 if p == 0 else 0))
|
||||
@ -512,7 +395,6 @@ class RasterView:
|
||||
alpha_img = alpha_img.point(lambda p: 0 if p == 0 else 255)
|
||||
|
||||
# colorize final image
|
||||
print_verbose('Colorizing image...')
|
||||
fill_color = (1.0, 1.0, 1.0)
|
||||
result = Image.merge("RGBA", [
|
||||
all_lines.point(lambda p: int(fill_color[0] * p + line_color[0] * (255.0 - p))),
|
||||
@ -521,16 +403,6 @@ class RasterView:
|
||||
alpha_img
|
||||
])
|
||||
|
||||
# set clip color
|
||||
if not fast_render:
|
||||
clip_color = (0.5, 0.5, 0.5)
|
||||
colorized_clip_img = Image.merge("RGB", [
|
||||
clip_img.point(lambda p: int(clip_color[0] * (255.0 - p))),
|
||||
clip_img.point(lambda p: int(clip_color[1] * (255.0 - p))),
|
||||
clip_img.point(lambda p: int(clip_color[2] * (255.0 - p)))
|
||||
])
|
||||
result.paste(colorized_clip_img, clip_img.point(lambda p: 255 - p))
|
||||
|
||||
# crop 1px borders
|
||||
result = result.crop((1, 1, result.size[0] - 1, result.size[1] - 1))
|
||||
|
||||
|
@ -7,8 +7,6 @@ import TechDraw, TechDrawGui
|
||||
from PySide import QtGui, QtCore
|
||||
TDG = TechDrawGui
|
||||
|
||||
from ahb_material import Material
|
||||
|
||||
class CursorItem(QtGui.QGraphicsItem):
|
||||
def __init__(self, parent = None, view = None):
|
||||
super().__init__(parent)
|
||||
@ -79,16 +77,10 @@ class TechDrawExtensions:
|
||||
|
||||
enable_selected_part_highlight = False # disable for now, for performance reasons
|
||||
|
||||
initialized_documents = []
|
||||
|
||||
def __init__(self):
|
||||
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
|
||||
workbench.docObserver.onObjectTypeChanged('balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop))
|
||||
workbench.docObserver.onObjectTypeSelected('balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point))
|
||||
workbench.docObserver.onDocumentEvent('techdrawext_doc_event', lambda doc, event: self.onDocumentEvent(doc, event))
|
||||
|
||||
if App.ActiveDocument is not None:
|
||||
self.onDocumentEvent(App.ActiveDocument, 'activate')
|
||||
|
||||
def repaint(self, view, fast_render = True):
|
||||
self.views_to_repaint[view] = fast_render
|
||||
@ -96,7 +88,6 @@ class TechDrawExtensions:
|
||||
|
||||
def _do_repaint(self):
|
||||
from ahb_raster_view import RasterView
|
||||
import Draft
|
||||
|
||||
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
|
||||
|
||||
@ -136,28 +127,44 @@ class TechDrawExtensions:
|
||||
|
||||
overlay_frame_name = view.Label + "_frame"
|
||||
overlay_frame = doc.getObject(overlay_frame_name)
|
||||
if overlay_frame is not None:
|
||||
doc.removeObject(overlay_frame.Name)
|
||||
#overlay_frame = Draft.makeWire(points, closed=False, face=False, support=None)
|
||||
overlay_frame = doc.addObject("Part::Part2DObjectPython", overlay_frame_name)
|
||||
Draft.Wire(overlay_frame)
|
||||
pos = raster_view.projectImageViewPointTo3D(App.Vector(0,0,0))
|
||||
pos2 = raster_view.projectImageViewPointTo3D(App.Vector(0.001,0.001,1))
|
||||
overlay_frame.Points = [pos, pos2]
|
||||
Draft.ViewProviderWire(overlay_frame.ViewObject)
|
||||
overlay_frame.recompute()
|
||||
|
||||
if overlay_frame is None:
|
||||
import Draft
|
||||
'''
|
||||
points = [App.Vector(0.0, 0.0, 0.0), App.Vector(1.01, 0, 0)]
|
||||
obj1 = Draft.makeWire(points, closed=False, face=False, support=None)
|
||||
|
||||
points = [App.Vector(200.0, 0.0, 0.0), App.Vector(201.01, 0, 0)]
|
||||
obj2 = Draft.makeWire(points, closed=False, face=False, support=None)
|
||||
|
||||
#overlay_frame = Draft.upgrade([], delete=True)
|
||||
#overlay_frame.Label = overlay_frame_name
|
||||
overlay_frame = App.ActiveDocument.addObject("Part::Part2DObjectPython", overlay_frame_name)
|
||||
Draft.Block(overlay_frame)
|
||||
overlay_frame.Components = [obj1, obj2]
|
||||
Draft.ViewProviderDraftPart(overlay_frame.ViewObject)
|
||||
doc.removeObject(obj1.Name)
|
||||
doc.removeObject(obj2.Name)'''
|
||||
|
||||
#overlay_frame = Draft.makeWire(points, closed=False, face=False, support=None)
|
||||
overlay_frame = App.ActiveDocument.addObject("Part::Part2DObjectPython", overlay_frame_name)
|
||||
Draft.Wire(overlay_frame)
|
||||
pos = raster_view.projectImageViewPointTo3D(App.Vector(0,0,0))
|
||||
pos2 = raster_view.projectImageViewPointTo3D(App.Vector(0.001,0.001,1))
|
||||
overlay_frame.Points = [pos, pos2]
|
||||
Draft.ViewProviderWire(overlay_frame.ViewObject)
|
||||
overlay_frame.recompute()
|
||||
|
||||
overlay_frame2_name = view.Label + "_frame2"
|
||||
overlay_frame2 = doc.getObject(overlay_frame2_name)
|
||||
if overlay_frame2 is not None:
|
||||
doc.removeObject(overlay_frame2.Name)
|
||||
overlay_frame2 = doc.addObject("Part::Part2DObjectPython", overlay_frame2_name)
|
||||
Draft.Wire(overlay_frame2)
|
||||
pos = raster_view.projectImageViewPointTo3D(App.Vector(1,1,0))
|
||||
pos2 = raster_view.projectImageViewPointTo3D(App.Vector(1.001,1.001,1))
|
||||
overlay_frame2.Points = [pos, pos2]
|
||||
Draft.ViewProviderWire(overlay_frame2.ViewObject)
|
||||
overlay_frame2.recompute()
|
||||
if overlay_frame2 is None:
|
||||
overlay_frame2 = App.ActiveDocument.addObject("Part::Part2DObjectPython", overlay_frame2_name)
|
||||
Draft.Wire(overlay_frame2)
|
||||
pos = raster_view.projectImageViewPointTo3D(App.Vector(1,1,0))
|
||||
pos2 = raster_view.projectImageViewPointTo3D(App.Vector(1.001,1.001,1))
|
||||
overlay_frame2.Points = [pos, pos2]
|
||||
Draft.ViewProviderWire(overlay_frame2.ViewObject)
|
||||
overlay_frame2.recompute()
|
||||
|
||||
overlay.Source = [overlay_frame, overlay_frame2]
|
||||
|
||||
@ -508,15 +515,14 @@ class TechDrawExtensions:
|
||||
obj = self.getBalloonSourcePart(balloon)
|
||||
path = self.getBalloonSourcePartPath(balloon)
|
||||
|
||||
if obj is not None:
|
||||
objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj, path)
|
||||
partDisplayName = 'Inconnu' if obj is None else self.getPartDisplayName(obj)
|
||||
|
||||
balloon.OriginX = objectCenterView.x + balloon.Assembly_handbook_OriginOffsetX
|
||||
balloon.OriginY = objectCenterView.y + balloon.Assembly_handbook_OriginOffsetY
|
||||
|
||||
partDisplayName = 'Inconnu' if obj is None else self.getPartDisplayName(obj)
|
||||
balloon.Text = partDisplayName
|
||||
objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj, path)
|
||||
|
||||
balloon.OriginX = objectCenterView.x + balloon.Assembly_handbook_OriginOffsetX
|
||||
balloon.OriginY = objectCenterView.y + balloon.Assembly_handbook_OriginOffsetY
|
||||
|
||||
balloon.Text = partDisplayName
|
||||
balloon.ViewObject.Font = 'DejaVu Sans'
|
||||
balloon.ViewObject.Fontsize = 4
|
||||
balloon.BubbleShape = 'Inspection'
|
||||
@ -640,24 +646,8 @@ class TechDrawExtensions:
|
||||
QTimer.singleShot(10, restoreKeepUpdated)
|
||||
|
||||
def refreshOverlays(self, page, callback = None):
|
||||
import os
|
||||
|
||||
for view in page.Views:
|
||||
if view.TypeId == 'TechDraw::DrawViewPart' and 'Assembly_handbook_RasterView' in view.PropertiesList and view.Assembly_handbook_RasterView:
|
||||
view.purgeTouched() # make sure we don't trigger rendering of source views (this is awfully slow and doesn't even work for a lot of models)
|
||||
|
||||
doc = page.Document
|
||||
for image in page.Views:
|
||||
if image.TypeId == 'TechDraw::DrawViewImage':
|
||||
folder_name = '/' + os.path.basename(doc.FileName).replace('.FCStd', '') + '_raster/'
|
||||
if folder_name in image.ImageFile:
|
||||
full_path = doc.FileName.replace('.FCStd', '') + '_raster/' + image.ImageFile.split(folder_name)[1]
|
||||
if image.ImageFile != full_path:
|
||||
image.ImageFile = full_path
|
||||
|
||||
if page.KeepUpdated:
|
||||
if callback:
|
||||
callback()
|
||||
callback()
|
||||
else:
|
||||
page.KeepUpdated = True
|
||||
def restoreKeepUpdated():
|
||||
@ -782,48 +772,3 @@ class TechDrawExtensions:
|
||||
cache = ViewCache()
|
||||
self.view_cache[view] = cache
|
||||
return cache
|
||||
|
||||
def onDocumentEvent(self, doc, event):
|
||||
if event == 'activate':
|
||||
if doc not in self.initialized_documents:
|
||||
self.initialized_documents.append(doc)
|
||||
self.initializeDocument(doc)
|
||||
elif event == 'deleted':
|
||||
if doc in self.initialized_documents:
|
||||
self.initialized_documents.remove(doc)
|
||||
|
||||
def initializeDocument(self, doc):
|
||||
def doInit():
|
||||
main_part = None
|
||||
try:
|
||||
for obj in doc.Objects:
|
||||
if obj.TypeId == 'TechDraw::DrawPage':
|
||||
self.onPageLoaded(obj)
|
||||
|
||||
main_parts = doc.getObjectsByLabel(doc.Name)
|
||||
if len(main_parts) == 1:
|
||||
main_part = main_parts[0]
|
||||
except:
|
||||
pass
|
||||
|
||||
if main_part is not None:
|
||||
self.initPartMetadata(main_part)
|
||||
|
||||
QTimer.singleShot(0, doInit)
|
||||
|
||||
def initPartMetadata(self, part):
|
||||
current_material = 'Unknown'
|
||||
if 'Assembly_handbook_Material' in part.PropertiesList:
|
||||
current_material = part.Assembly_handbook_Material
|
||||
else:
|
||||
part.addProperty("App::PropertyEnumeration", "Assembly_handbook_Material", "Assembly_handbook")
|
||||
material_list = ['Unknown'] + Material.GetMaterialIDs()
|
||||
part.Assembly_handbook_Material = material_list
|
||||
part.Assembly_handbook_Material = material_list.index(current_material) if current_material in material_list else 0
|
||||
|
||||
if 'Assembly_handbook_Weight' not in part.PropertiesList:
|
||||
part.addProperty("App::PropertyFloat", "Assembly_handbook_Weight", "Assembly_handbook", 'Part weight in grams. Set a negative number if weight is unknown.')
|
||||
part.Assembly_handbook_Weight = -1
|
||||
|
||||
def onPageLoaded(self, page):
|
||||
self.refreshOverlays(page)
|
||||
|
Loading…
Reference in New Issue
Block a user