diff --git a/ahb_techdraw_extensions.py b/ahb_techdraw_extensions.py index 373b957..1d570fc 100644 --- a/ahb_techdraw_extensions.py +++ b/ahb_techdraw_extensions.py @@ -121,7 +121,11 @@ class TechDrawExtensions: page.Views = new_views_list image_file_name = doc.FileName.replace('.FCStd', '') + '_raster/' + view.Name + '.png' - self.rasterizeView(view, image_file_name) + image_scale = self.rasterizeView(view, image_file_name) + image.ImageFile = "" + image.Scale = image_scale + image.X = view.X #- image_center[0]/10.0*image_scale + image.Y = view.Y #+ image_center[1]/10.0*image_scale image.ImageFile = image_file_name # TODO: see if it's possible to set a relative path image.recompute() else: @@ -230,6 +234,7 @@ class TechDrawExtensions: def rasterizeView(self, view, image_file_name): from pivy import coin import os + from PIL import Image, ImageDraw, ImageChops print('Rasterizing ' + view.Label + " to " + image_file_name + "...") @@ -257,9 +262,55 @@ class TechDrawExtensions: docView.fitAll() + viewVolume = cam.getViewVolume(0.0) + docView.saveImage(image_file_name, 4096, 4096, "#ffffff") + def project3dPointToViewport(p3d): + YDirection = view.Direction.cross(view.XDirection) + sb_offset = viewVolume.projectToScreen(coin.SbVec3f(0,0,0)) + offset = App.Vector(sb_offset[0], sb_offset[1], 0) + #print('offset', offset) + p2d = App.Vector(view.XDirection.dot(p3d)/viewVolume.getWidth() + offset.x, YDirection.dot(p3d)/viewVolume.getHeight() + offset.y, 0) + return p2d + + p2dA = project3dPointToViewport(App.Vector(0,0,0)) + p2dB = project3dPointToViewport(view.XDirection) + # page_mm = (p2dB.x - p2dA.x) * resX / 100 * PNGscale = viewScale + imageScale = view.Scale / (p2dB.x - p2dA.x) / 4096.0 * 10 + print('imageScale', imageScale) + + with Image.open(image_file_name) as img: + original_size = img.size + + bg = Image.new(img.mode, img.size, '#ffffff') # fills an image with the background color + diff = ImageChops.difference(img, bg) # diff between the actual image and the background color + bbox = diff.getbbox() # finds border size (non-black portion of the image) + print(bbox) + #image_center = (bbox[0] + (bbox[2] - bbox[0])/2 - img.size[0]/2, bbox[1] + (bbox[3] - bbox[1])/2 - img.size[1]/2) + #print(image_center) + img = img.crop(bbox) + + draw = ImageDraw.Draw(img) + + def debugPoint(p3d): + p2d = project3dPointToViewport(p3d) + pp = App.Vector(p2d.x * original_size[0] - bbox[0], (1.0-p2d.y) * original_size[1] - bbox[1]) + #print('pp', pp) + + len = 100 + draw.line([(pp.x, pp.y-len), (pp.x, pp.y+len)], fill=128, width = 7) + draw.line([(pp.x-len, pp.y), (pp.x+len, pp.y)], fill=128, width = 7) + + #debugPoint(App.Vector(-12.5, 37.5, 25.0)) + #debugPoint(App.Vector(-12.5, -1387.5, 25.0)) + #debugPoint(App.Vector(131.23702882966705, -655.0000021095163, 145.21130178331268)) + + img.save(image_file_name) + App.closeDocument(doc.Name) + + return imageScale def updateBalloonCursor(self, view): selected_balloons = [] @@ -406,7 +457,7 @@ class TechDrawExtensions: view = balloon.SourceView obj = self.getBalloonSourcePart(balloon) - partDisplayName = self.getPartDisplayName(obj) + partDisplayName = 'Inconnu' if obj is None else self.getPartDisplayName(obj) objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj)