Macro Unfold Box/it: Difference between revisions

From FreeCAD Documentation
No edit summary
(Updating to match new version of source page)
 
Line 1: Line 1:
<languages/>
<languages/>

<div class="mw-translate-fuzzy">
{{Macro/it
{{Macro/it
|Name=Macro Unfold Box
|Name=Macro Unfold Box
Line 11: Line 13:
|Download=[https://www.freecadweb.org/wiki/images/e/e4/Macro_Unfold_Box.png ToolBar Icon]
|Download=[https://www.freecadweb.org/wiki/images/e/e4/Macro_Unfold_Box.png ToolBar Icon]
}}
}}
</div>


==Description==

<div class="mw-translate-fuzzy">
==Descrizione==
==Descrizione==
Questa macro permette di creare lo sviluppo delle superfici di un solido con facce piane, di disegnare le facce in una pagina e di inserire il disegno in un foglio A3 o A4.
Questa macro permette di creare lo sviluppo delle superfici di un solido con facce piane, di disegnare le facce in una pagina e di inserire il disegno in un foglio A3 o A4.
</div>


[[File:Macro_unfoldBox1.png|480px]]
<div class="mw-translate-fuzzy">
[[File:Macro_unfoldBox1.png|480px]]
[[File:Macro_unfoldBox1.png|480px]]
{{Caption|Macro_unfoldBox}}
{{Caption|Macro_unfoldBox}}
</div>


== Installation ==

<div class="mw-translate-fuzzy">
== Installazione ==
== Installazione ==
Secondo quanto descritto in [http://forum.freecadweb.org/viewtopic.php?f=17&t=4587 Macro for unfolding box surfaces]
Secondo quanto descritto in [http://forum.freecadweb.org/viewtopic.php?f=17&t=4587 Macro for unfolding box surfaces]
Line 25: Line 38:
*'''Windows''' : C:\Program Files\FreeCAD0.13
*'''Windows''' : C:\Program Files\FreeCAD0.13
Aggiungere i modelli del foglio : A3_Landscape_Empty.svg A3_Landscape.svg A4_Landscape_Empty.svg A4_Landscape.svg
Aggiungere i modelli del foglio : A3_Landscape_Empty.svg A3_Landscape.svg A4_Landscape_Empty.svg A4_Landscape.svg
</div>


Forum topic: [http://forum.freecadweb.org/viewtopic.php?f=17&t=4587 Macro for unfolding box surfaces].

== Options ==

<div class="mw-translate-fuzzy">
== Opzioni ==
== Opzioni ==
* Scala manuale o automatica
* Scala manuale o automatica
Line 32: Line 51:
* Raggruppamento dei disegni nella stessa pagina se è possibile.
* Raggruppamento dei disegni nella stessa pagina se è possibile.
* Unire le facce su un lato.
* Unire le facce su un lato.
</div>


[[File:Macro_UnFoldBox_start_form.png|Macro_unfoldBox]]

== Usage ==

<div class="mw-translate-fuzzy">
== Utilizzo ==
== Utilizzo ==
# Selezionare un oggetto creato con Part, ad es. un oggetto creato con Loft.
# Selezionare un oggetto creato con Part, ad es. un oggetto creato con Loft.
Line 38: Line 63:
# Selezionare le supefici
# Selezionare le supefici
# Eseguire la macro
# Eseguire la macro
</div>

The unfolding algorithm will place the faces on the XY plane, however it seldom does the unfolding correctly. Thus some manual post-processing is needed to get to desired result, as shown in the picture below.

[[File:MacroUnFoldBoxInstruction.png|Macro_unfoldBox]]

Starting point is a box like the upper left picture.

# Select all faces and run the macro.
# Orient to Top View.
# Toggle the visibility on the created set of faces, the individual clones are to be visible, not the compound.
# Use [[Draft_Move|Draft Move]] and [[Draft_Rotate|Draft Rotate]] to reposition the unfolded cloned faces (a handful of clicks per face when snapping is used).
# The drawing updates automatically to the new positions.

The finished result is shown in the lower right picture above.


==Script==
==Script==
Line 45: Line 85:
'''Macro_unfoldBox.FCMacro'''
'''Macro_unfoldBox.FCMacro'''


<!--DO NOT CHANGE THE <pre> </pre> TAG, code contains pipe or space character which breaks the wiki macro-->
<!-- Do not remove the pair of <nowiki> </nowiki> tags because the special symbols won't be parsed correctly -->
<pre>
{{MacroCode|code=
# -*- coding: utf-8 -*-
<nowiki>
"""
'''FreeCAD Macro unfoldBox.
FreeCAD Macro unfoldBox.


Unfolding of planar surfaces
Unroll of a ruled surface
"""
'''
#####################################
##################################################
# SEE https://wiki.freecadweb.org/Macro_Unfold_Box.
# SEE https://wiki.freecadweb.org/Macro_Unfold_Box
# ***************************************************************************
# ***************************************************************************
# * *
# * *
# * Copyright (c) 2013 - DoNovae/Herve BAILLY <hbl13@donovae.com> *
# * Copyright (c) 2013 - DoNovae/Herve BAILLY <hbl13@donovae.com> *
# * Copyright (c) 2022 - heda <heda@fc-forum> *
# * *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * This program is free software; you can redistribute it and/or modify *
Line 76: Line 118:
# ***************************************************************************
# ***************************************************************************


__Name__ = 'Unfold Box'
import os
__Comment__ = 'Unfolds planar surfaces and draws them on a page.'
import math
__Author__ = 'Hervé B., heda'
__Version__ = '1.1'
__Date__ = '2022-07-28'
__License__ = 'LGPL-2.0-or-later'
__Web__ = 'https://wiki.freecad.org/Macro_Unfold_Box'
__Wiki__ = 'https://wiki.freecad.org/Macro_Unfold_Box'
__Icon__ = ''
__Help__ = ('Select surfaces, Explode them (cf Draft menu Downgrade), '
'Select the exploded surfaces, Execute the macro')
__Status__ = ''
__Requires__ = ''
__Communication__ = ''
__Files__ = ''

__doc__ = """
select a face, or several and run the macro.
a shell/solid needs to be draft/downgraded to get the faces as separate objects

the macro is intended to unfold a set of planar faces,
in current state, most of the times it does not place the faces correctly.
thus one needs to do the final placement of the clones manually, with Draft/Move & Rotate
regardless, the macro saves a lot of clicks compared to a fully manual unfold

settings are not context aware, all settings not applicable are ignored.
as example, using autoscaling ignores the scale-value in the form

sew is always enabled, thus the drawing
is always the sewed shape on a single page


v1.1 (2022-07-28) py3/qt5 compat, cosmetic code changes,
minor code tweaks for evolved fc-api
added option to skip drawing, made layout engine with techdraw,
the drawing wb layout engine is now called "legacy"
v1.0.1 (2020-03-10) - on wiki
v1.0 (2013-09-14) - on wiki

note:
- unfolding occasionally work as expected, but mostly not
- anyone is free to improve functionality

"""

import os, math


import FreeCAD, FreeCADGui, Draft
from PySide import QtGui, QtCore
from PySide import QtGui, QtCore

from FreeCAD import Base
import FreeCAD, FreeCADGui
import Draft

Vector = FreeCAD.Base.Vector
Units = FreeCAD.Units
PrintMessage = FreeCAD.Console.PrintMessage
PrintError = FreeCAD.Console.PrintError

fields_l = []
fields_l = []
unroll_l = []
unroll_l = []
dwgtpllegacy = 'Mod/Drawing/Templates'
dwgtpl = 'Mod/TechDraw/Templates'



#####################################
#####################################
# Functions
### Functions
#####################################
#####################################




def errorDialog(msg):
def errorDialog(msg):
diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical, u'Error Message',msg)
diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical, 'Error Message', msg)
diag.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
diag.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
diag.exec_()
diag.exec_()
Line 97: Line 193:


def proceed():
def proceed():
QtGui.qApp.setOverrideCursor(QtCore.Qt.WaitCursor)
QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)


FreeCAD.Console.PrintMessage('===========================================\n')
PrintMessage('===========================================\n')
FreeCAD.Console.PrintMessage('unfoldBox: start.\n')
PrintMessage('unfoldBox: start.\n')
try:
try:
file_name = fields_l[0].text()
file_name = fields_l[0].text()
makedwg = makedwg_check.isChecked()
leglay = leglay_check.isChecked()
scale = float(fields_l[1].text())
scale = float(fields_l[1].text())
scale_auto = scale_check.isChecked()
scale_auto = scale_check.isChecked()
Line 109: Line 207:
onedrawing = onedrawing_check.isChecked()
onedrawing = onedrawing_check.isChecked()
sewed = sewed_check.isChecked()
sewed = sewed_check.isChecked()
FreeCAD.Console.PrintMessage('unfoldBox.file_name: '+file_name+'\n')
PrintMessage('unfoldBox.file_name: {}\n'.format(file_name))
FreeCAD.Console.PrintMessage('unfoldBox.scale: '+str(scale)+'\n')
PrintMessage('unfoldBox.makedwg: {}\n'.format(makedwg))
FreeCAD.Console.PrintMessage('unfoldBox.scale_check: '+str(scale_auto)+'\n')
PrintMessage('unfoldBox.leglay: {}\n'.format(leglay))
FreeCAD.Console.PrintMessage('unfoldBox.a3_check: '+str(a3)+'\n')
PrintMessage('unfoldBox.scale: {}\n'.format(scale))
FreeCAD.Console.PrintMessage('unfoldBox.cartridge: '+str(cartridge)+'\n')
PrintMessage('unfoldBox.scale_check: {}\n'.format(scale_auto))
FreeCAD.Console.PrintMessage('unfoldBox.onedrawing: '+str(onedrawing)+'\n')
PrintMessage('unfoldBox.a3_check: {}\n'.format(a3))
FreeCAD.Console.PrintMessage('unfoldBox.sewed: '+str(sewed)+'\n')
PrintMessage('unfoldBox.cartridge: {}\n'.format(cartridge))
PrintMessage('unfoldBox.onedrawing: {}\n'.format(onedrawing))
PrintMessage('unfoldBox.sewed: {}\n'.format(sewed))
except:
except:
msg = 'unfoldBox: wrong inputs...\n'
msg = 'unfoldBox: wrong inputs...\n'
FreeCAD.Console.PrintError(msg)
PrintError(msg)
errorDialog(msg)
errorDialog(msg)


QtGui.qApp.restoreOverrideCursor()
QtGui.QApplication.restoreOverrideCursor()
DialogBox.hide()
DialogBox.hide()

#
# Get selection
## Get selection
#
sel = FreeCADGui.Selection.getSelection()
sel = FreeCADGui.Selection.getSelection()
if not sel or len(sel) < 2:
PrintMessage('unfoldBox: requires at least 2 selected faces, ending.\n')
return

doc = FreeCAD.ActiveDocument

objnames_l=[]
objnames_l=[]
grp = FreeCAD.activeDocument().addObject('App::DocumentObjectGroup', str(file_name))
grp = doc.addObject('App::DocumentObjectGroup', str(file_name))
for objid in range(sel.__len__()):
for objid in range(len(sel)):
obj = Draft.clone(sel[objid])
obj = Draft.make_clone(sel[objid])
grp.addObject(obj)
grp.addObject(obj)
objnames_l.append([ obj, sel[objid].Name ])
objnames_l.append([ obj, sel[objid].Name ])


unfold = unfoldBox()
doc.recompute()
unfold = unfoldBox(doc)
if sewed :
if sewed:
objnames_l = unfold.done(objnames_l)
objnames_l = unfold.done(objnames_l)
grp.addObject(objnames_l[0][0])
grp.addObject(objnames_l[0][0])
else:
else:
for objid in range(objnames_l.__len__()):
for objid in range(len(objnames_l)):
unfold.moveXY(objnames_l[objid][0])
unfold.moveXY(objnames_l[objid][0])


id_ = 0
if not makedwg:
return
while objnames_l.__len__() > 0:
draw = Drawing2d(scale, scale_auto, a3, cartridge, onedrawing, FreeCAD.activeDocument().Name, 'Page'+str(id_))
objnames_l = draw.all_(objnames_l)
id_ = id_+1
FreeCAD.Console.PrintMessage('unfoldBox: obj_l= '+str(objnames_l.__len__())+'\n')


if leglay:
FreeCAD.Console.PrintMessage('unfoldBox: end.\n')
idx = 0
FreeCAD.Console.PrintMessage('===========================================\n')
while len(objnames_l) > 0:
draw = Drawing2dLegacy(scale, scale_auto, a3,
cartridge, onedrawing,
doc.Name, 'Page'+str(idx))
objnames_l = draw.all_(objnames_l)
idx += 1
PrintMessage('unfoldBox: obj_l= {}\n'.format(len(objnames_l)))
else:
draw = Drawing2d(scale, scale_auto, a3, cartridge)
draw.drawpage(objnames_l[0], "{}Page".format(file_name))

PrintMessage('unfoldBox: end.\n')
PrintMessage('===========================================\n')




Line 156: Line 271:
DialogBox.hide()
DialogBox.hide()



def getType(obj):
return type(obj).__name__

def mkmm(l):
return Units.Quantity(l, Units.Length)


class unfoldBox:
class unfoldBox:
def __init__(self):
def __init__(self, doc):
FreeCAD.Console.PrintMessage('unfoldBox.unfoldBox\n')
PrintMessage('unfoldBox.unfoldBox\n')
self.doc = doc
self.LIMIT = 0.0001
self.LIMIT = 0.0001


def done(self, objnames_l):
def done(self, objnames_l):
tree_l = self.makeTree(objnames_l)
tree_l = self.makeTree(objnames_l)
for id_ in range(objnames_l.__len__()):
for idx in range(len(objnames_l)):
face = objnames_l[id_]
face = objnames_l[idx]
self.moveXY(face[0])
self.moveXY(face[0])
self.sew(objnames_l, tree_l)
self.sew(objnames_l, tree_l)
return self.fusion(objnames_l)
return self.fusion(objnames_l)

def getEndPoints(self, edge):
return [v.Point for v in edge.Vertexes]


def makeTree(self, objnames_l):
def makeTree(self, objnames_l):
# Initialisation of tree_l.
## Initialisation of tree_l.
tree_l = []
tree_l = []
for k in range(objnames_l.__len__()):
for k in range(len(objnames_l)):
facek = objnames_l[k][0]
facek = objnames_l[k][0]
facekEdges = facek.Shape.Edges
facek_l = []
facek_l = []
for i in range(facek.Shape.Edges.__len__()):
for i in range(len(facekEdges)):
if False and type(facek.Shape.Edges[i].Curve).__name__ != 'GeomLineSegment':
if False and getType(facekEdges[i].Curve) != 'GeomLineSegment':
## this is a no-op...
facek_l.append([-1, -1])
facek_l.append([-1, -1])
else:
else:
# Search face link to the ith edge
## Search face link to the ith edge
vki0 = facek.Shape.Edges[i].Curve.StartPoint
#vki0 = facekEdges[i].Curve.StartPoint
vki1 = facek.Shape.Edges[i].Curve.EndPoint
#vki1 = facekEdges[i].Curve.EndPoint
vki0, vki1 = self.getEndPoints(facekEdges[i])
found = False
found = False
for l in range(k+1, objnames_l.__len__()):
for l in range(k+1, len(objnames_l)):
facel = objnames_l[l][0]
facel = objnames_l[l][0]
for j in range(facel.Shape.Edges.__len__()):
facelEdges = facel.Shape.Edges
vlj0 = facel.Shape.Edges[j].Curve.StartPoint
for j in range(len(facelEdges)):
vlj1 = facel.Shape.Edges[j].Curve.EndPoint
#vlj0 = facelEdges[j].Curve.StartPoint
if vki0.__eq__(vlj0) and vki1.__eq__(vlj1):
#vlj1 = facelEdges[j].Curve.EndPoint
vlj0, vlj1 = self.getEndPoints(facelEdges[j])
if (vki0.isEqual(vlj0, self.LIMIT)
and vki1.isEqual(vlj1, self.LIMIT)):
arelinked = False
arelinked = False
isfacek = False
isfacek = isfacel = False
isfacel = False
for kk in range(k-1):
for kk in range(k-1):
for ii in range(tree_l[kk].__len__()):
for ii in range(len(tree_l[kk])):
if tree_l[kk][ii][0] == k:
isfacek = tree_l[kk][ii][0] == k
isfacek = True
isfacel = tree_l[kk][ii][0] == l
if tree_l[kk][ii][0] == l:
isfacel = True
if isfacek and isfacel:
if isfacek and isfacel:
arelinked = True
arelinked = True
Line 215: Line 343:
def sew(self, objnames_l, tree_l):
def sew(self, objnames_l, tree_l):
placed_l = []
placed_l = []
for k in range(tree_l.__len__()):
for k in range(len(tree_l)):
iskplaced = False
iskplaced = False
for p in range(placed_l.__len__()):
for p in range(len(placed_l)):
if placed_l[p] == k:
iskplaced = placed_l[p] == k
iskplaced = True
if not iskplaced:
if not iskplaced:
placed_l.append(k)
placed_l.append(k)
facek = tree_l[k]
facek = tree_l[k]
objk = objnames_l[k][0]
objk = objnames_l[k][0]
for i in range(facek.__len__()):
for i in range(len(facek)):
edgeki = facek[i]
edgeki = facek[i]
l = edgeki[0]
l, j = edgeki[:2]
j = edgeki[1]
islplaced = False
islplaced = False
for p in range(placed_l.__len__()):
for p in range(len(placed_l)):
if placed_l[p] == l:
if placed_l[p] == l:
islplaced = True
islplaced = True
Line 237: Line 363:
if l >= 0 and not (islplaced and iskplaced):
if l >= 0 and not (islplaced and iskplaced):
iskplaced = True
iskplaced = True
# Move facel.edgelj to facek.edgeki.
## Move facel.edgelj to facek.edgeki.
objl = objnames_l[l][0]
objl = objnames_l[l][0]
vki0 = objk.Shape.Edges[i].Curve.StartPoint
#vki0 = objk.Shape.Edges[i].Curve.StartPoint
vki1 = objk.Shape.Edges[i].Curve.EndPoint
#vki1 = objk.Shape.Edges[i].Curve.EndPoint
vlj0 = objl.Shape.Edges[j].Curve.StartPoint
vki0, vki1 = self.getEndPoints(objk.Shape.Edges[i])
vlj1 = objl.Shape.Edges[j].Curve.EndPoint
#vlj0 = objl.Shape.Edges[j].Curve.StartPoint
#vlj1 = objl.Shape.Edges[j].Curve.EndPoint
vlj0, vlj1 = self.getEndPoints(objk.Shape.Edges[j])
vk = vki1.sub(vki0)
vk = vki1.sub(vki0)
vl = vlj1.sub(vlj0)
vl = vlj1.sub(vlj0)
alpk = vk.getAngle(vl)*180/math.pi
alpk = vk.getAngle(vl) * 180 / math.pi
alpl = vl.getAngle(vk)*180/math.pi
alpl = vl.getAngle(vk) * 180 / math.pi
self.isPlanZ(objk)
self.isPlanZ(objk)
if islplaced:
if islplaced:
Line 254: Line 382:
self.isPlanZ(objk)
self.isPlanZ(objk)


if math.fabs(vk.dot(FreeCAD.Base.Vector(-vl.y, vl.x, 0))) > self.LIMIT:
if math.fabs(vk.dot(Vector(-vl.y, vl.x, 0))) > self.LIMIT:
if islplaced:
if islplaced:
Draft.rotate(objk, -alpl, vlj0, self.vecto(vl, vk))
Draft.rotate(objk, -alpl, vlj0, self.vecto(vl, vk))
Line 261: Line 389:
elif vk.dot(vl) < 0:
elif vk.dot(vl) < 0:
if islplaced:
if islplaced:
Draft.rotate(objk, 180, vlj0, self.vecto(vl, FreeCAD.Base.Vector(-vl.y, vl.x, 0)))
Draft.rotate(objk, 180, vlj0, self.vecto(vl, Vector(-vl.y, vl.x, 0)))
else:
else:
Draft.rotate(objl, 180, vki0, self.vecto(vk, FreeCAD.Base.Vector(-vk.y, vk.x, 0)))
Draft.rotate(objl, 180, vki0, self.vecto(vk, Vector(-vk.y, vk.x, 0)))
# Verifications.
## Verifications.
vki0 = objk.Shape.Edges[i].Curve.StartPoint
#vki0 = objk.Shape.Edges[i].Curve.StartPoint
vki1 = objk.Shape.Edges[i].Curve.EndPoint
#vki1 = objk.Shape.Edges[i].Curve.EndPoint
vlj0 = objl.Shape.Edges[j].Curve.StartPoint
vki0, vki1 = self.getEndPoints(objk.Shape.Edges[i])
vlj1 = objl.Shape.Edges[j].Curve.EndPoint
#vlj0 = objl.Shape.Edges[j].Curve.StartPoint
#vlj1 = objl.Shape.Edges[j].Curve.EndPoint
vli0, vli1 = self.getEndPoints(objk.Shape.Edges[j])
vk = vki1.sub(vki0)
vk = vki1.sub(vki0)
vl = vlj1.sub(vlj0)
vl = vlj1.sub(vlj0)
self.isPlanZ(objk)
self.isPlanZ(objk)


# Flip or not.
## Flip or not.
L = max(objl.Shape.BoundBox.XMax, objk.Shape.BoundBox.XMax) - min(objl.Shape.BoundBox.XMin, objk.Shape.BoundBox.XMin)
bbl, bbk = objl.Shape.BoundBox, objk.Shape.BoundBox
W = max(objl.Shape.BoundBox.YMax, objk.Shape.BoundBox.YMax) - min(objl.Shape.BoundBox.YMin, objk.Shape.BoundBox.YMin)
L = max(bbl.XMax, bbk.XMax) - min(bbl.XMin, bbk.XMin)
S1 = L*W
W = max(bbl.YMax, bbk.YMax) - min(bbl.YMin, bbk.YMin)
S1 = L * W
if islplaced:
if islplaced:
Draft.rotate(objk, 180, vlj0, vl)
Draft.rotate(objk, 180, vlj0, vl)
else:
else:
Draft.rotate(objl, 180, vki0, vk)
Draft.rotate(objl, 180, vki0, vk)
L = max(objl.Shape.BoundBox.XMax, objk.Shape.BoundBox.XMax) - min(objl.Shape.BoundBox.XMin, objk.Shape.BoundBox.XMin)
bbl, bbk = objl.Shape.BoundBox, objk.Shape.BoundBox
W = max(objl.Shape.BoundBox.YMax, objk.Shape.BoundBox.YMax) - min(objl.Shape.BoundBox.YMin, objk.Shape.BoundBox.YMin)
L = max(bbl.XMax, bbk.XMax) - min(bbl.XMin, bbk.XMin)
W = max(bbl.YMax, bbk.YMax) - min(bbl.YMin, bbk.YMin)
S2 = L * W
S2 = L * W
if (S2 <= S1):
if S2 <= S1:
if islplaced:
if islplaced:
Draft.rotate(objk, 180, vlj0, vl)
Draft.rotate(objk, 180, vlj0, vl)
Line 292: Line 424:


def isPlanZ(self, obj):
def isPlanZ(self, obj):
L = obj.Shape.BoundBox.XMax - obj.Shape.BoundBox.XMin
bb = obj.Shape.BoundBox
W = obj.Shape.BoundBox.YMax - obj.Shape.BoundBox.YMin
L = bb.XMax - bb.XMin
H = obj.Shape.BoundBox.ZMax - obj.Shape.BoundBox.ZMin
W = bb.YMax - bb.YMin
if H < self.LIMIT:
H = bb.ZMax - bb.ZMin
return True
return H < self.LIMIT
else:
return False




def fusion(self, objnames_l):
def fusion(self, objnames_l):
# Init.
## Init.
obj_l=[]
obj_l, objna_l =[], []
objna_l=[]
obj0, name = objnames_l[0]
objfuse = self.doc.addObject('Part::MultiFuse', 'Unfolding')
obj0 = objnames_l[0][0];name=objnames_l[0][1]
for k in range(len(objnames_l)):
objfuse = FreeCAD.activeDocument().addObject('Part::MultiFuse','Unfolding')
for k in range(objnames_l.__len__()):
objk = objnames_l[k][0]
objk = objnames_l[k][0]
obj_l.append(objk)
obj_l.append(objk)

objfuse.Shapes = obj_l
objfuse.Shapes = obj_l
FreeCAD.activeDocument().recompute()
self.doc.recompute()
objna_l.append([objfuse, name])
objna_l.append([objfuse, name])
return objna_l
return objna_l


def get2Vectors(self, shape):
def get2Vectors(self, shape):
v0 = FreeCAD.Base.Vector(0, 0, 0)
"""not used in v1.1"""
v1 = FreeCAD.Base.Vector(0, 0, 0)
v0, v1 = Vector(), Vector()


edges= shape.Edges
edges = shape.Edges
for id_ in range(edges.__len__()-1):
for idx in range(len(edges) - 1):
va = edges[id_].Curve.EndPoint.sub(edges[id_].Curve.StartPoint)
e1, e2 = edges[idx], edges[idx + 1]
vb = edges[id_+1].Curve.EndPoint.sub(edges[id_+1].Curve.StartPoint)
## .EndPoint errors out...
va = e1.Curve.EndPoint.sub(e1.Curve.StartPoint)
vb = e2.Curve.EndPoint.sub(e2.Curve.StartPoint)
if vb.sub(va).Length > v1.sub(v0).Length:
if vb.sub(va).Length > v1.sub(v0).Length:
v0 = self.vect_copy(va);v1=self.vect_copy(vb)
v0, v1 = self.vect_copy(va), self.vect_copy(vb)
return [v0, v1]
return [v0, v1]


def vecto(self, vect1, vect2):
def vecto(self, vect1, vect2):
'''Function vecto.'''
'''Function vecto.'''
v= FreeCAD.Base.Vector(0, 0, 0)
v = Vector()
v.x = vect1.y*vect2.z-vect1.z*vect2.y
v.x = vect1.y * vect2.z - vect1.z * vect2.y
v.y = vect1.z*vect2.x-vect1.x*vect2.z
v.y = vect1.z * vect2.x - vect1.x * vect2.z
v.z = vect1.x*vect2.y-vect1.y*vect2.x
v.z = vect1.x * vect2.y - vect1.y * vect2.x
return v
return v


def vect_copy(self, vect):
def vect_copy(self, vect):
'''Return a copy of vector.'''
'''Return a copy of vector.'''
v = vect.add(FreeCAD.Base.Vector(0, 0, 0))
return vect.add(Vector())
return v


def moveXY(self, obj):
def moveXY(self, obj):
# Move to origin
## Move to origin
Draft.move(obj, FreeCAD.Base.Vector(-obj.Shape.BoundBox.XMin, -obj.Shape.BoundBox.YMin, -obj.Shape.BoundBox.ZMin))
bb = obj.Shape.BoundBox
Draft.move(obj, Vector(-bb.XMin, -bb.YMin, -bb.ZMin))


# Find 2 vectors defining the plan of surface
## Find 2 vectors defining the plan of surface
tab = self.get2Vectors(obj.Shape)
#tab = self.get2Vectors(obj.Shape)
v0 = tab[0]
#v0, v1 = tab[:2]
v1 = tab[1]
#norm = self.vecto(v0, v1)
norm = self.vecto(v0, v1)
#norm.normalize()
## above gives null vector, and errors out...

## probably sprung out of some api-change over time
## probably part.line vs part.linesegment...
## a part.line does not have edge.Curve.StartPoint, or .EndPoint any more
## the semantics is to get first & second point from a line,
## so edge.Curve.value(edge.Curve.FirstParameter) should get the sought values
## however easier to just use edge.Vertexes...
## which appears to have same orientation

norm = obj.Shape.findPlane().Axis ## works for planar surfaces
#norm = obj.Shape.Surface.Axis ## gives worse results...
norm.normalize()
norm.normalize()


# Rotate.
## Rotate.
if math.fabs(norm.x) < self.LIMIT and math.fabs(norm.z) < self.LIMIT:
nx, ny, nz = (math.fabs(xyz) for xyz in norm)
if nx < self.LIMIT and nz < self.LIMIT:
Draft.rotate(obj, 90, FreeCAD.Base.Vector(0, 0, 0), FreeCAD.Base.Vector(1, 0, 0))
Draft.rotate(obj, 90, Vector(0, 0, 0), Vector(1, 0, 0))
elif math.fabs(norm.y) < self.LIMIT and math.fabs(norm.z) < self.LIMIT:
elif ny < self.LIMIT and nz < self.LIMIT:
Draft.rotate(obj, 90, FreeCAD.Base.Vector(0, 0, 0), FreeCAD.Base.Vector(0, 1, 0))
Draft.rotate(obj, 90, Vector(0, 0, 0), Vector(0, 1, 0))
else:
else:
# Rotate following the angle to the normal direction of the plan.
## Rotate following the angle to the normal direction of the plan.
oz= FreeCAD.Base.Vector(0, 0, 1)
oz = Vector(0, 0, 1)
alp = oz.getAngle(norm)*180/math.pi
alp = oz.getAngle(norm) * 180 / math.pi
Draft.rotate(obj, -alp, FreeCAD.Base.Vector(0, 0, 0), self.vecto(oz, norm))
vecto = self.vecto(oz, norm)
rotv = oz if vecto.isEqual(Vector(), self.LIMIT) else self.vecto(oz, norm)
#rotv = norm if vecto.isEqual(Vector(), self.LIMIT) else self.vecto(oz, norm)
## flipping here seems to give worse results as well...
Draft.rotate(obj, -alp, Vector(0, 0, 0), rotv)


# Move to z = 0.
## Move to z = 0.
Draft.move(obj, FreeCAD.Base.Vector(0, 0, -obj.Shape.BoundBox.ZMin))
Draft.move(obj, Vector(0, 0, -obj.Shape.BoundBox.ZMin))


class Scale:
"""keeps autoscaling to integers"""
def __init__(self, scale):
self.scale = scale if scale >= 1 else 1 / scale
self.scale = int(self.scale)
self.inverted = scale >= 1

def get(self):
if self.inverted:
return self.scale, '{}:1'.format(self.scale)
else:
return 1/self.scale, '1:{}'.format(self.scale)




class Drawing2d:
class Drawing2d:
def __init__(self, scale, scale_auto, a3, cartridge, onedrawing, drawing_name, page_name):
def __init__(self, scale, scale_auto, a3, cartridge):
"""techdraw based layout for one page only"""
self.scale = scale
self.scale_auto = scale_auto
self.a3 = a3
self.cartridge = cartridge
if a3:
self.WH = 420, 297
else:
self.WH = 297, 210
self.doc = FreeCAD.ActiveDocument

def newPage(self, page_name):
freecad_dir = os.path.join(FreeCAD.getResourceDir(), dwgtpl)
page = self.doc.addObject('TechDraw::DrawPage', page_name)
template = self.doc.addObject('TechDraw::DrawSVGTemplate', 'Template')
size = 'A3' if self.a3 else 'A4'
frame = 'TD' if self.cartridge else '_blank'
template.Template = freecad_dir + '/{}_Landscape{}.svg'.format(size, frame)
page.Template = template

return page

def drawpage(self, objname, page_name):
page = self.newPage(page_name)
faceset, name = objname

bb = faceset.Shape.BoundBox
## bb does not auto-update, have to pick up new bb after manipulation
if bb.YLength > bb.XLength: ## auto rotate
Draft.rotate(faceset, 90)
bb = faceset.Shape.BoundBox

Draft.move(faceset, Vector(-bb.XMin, -bb.YMin, 0))
bb = faceset.Shape.BoundBox

W, H = self.WH
xr, yr = W / bb.XLength, H / bb.YLength
adjust = 0.7 if self.cartridge else 0.85
scale = min(xr, yr) * adjust if self.scale_auto else self.scale
scale, scr = Scale(scale).get()
bb.scale(scale, scale, 1) ## faceset in xy-plane


TopView = self.doc.addObject('TechDraw::DrawViewPart', 'TopView')
page.addView(TopView)
TopView.Source = faceset
TopView.Direction = (0, 0, 1)
TopView.XDirection = (1, 0, 0)
TopView.Scale = scale

Text = self.doc.addObject('TechDraw::DrawViewAnnotation', name)
page.addView(Text)
Text.Text = name
Text.recompute() # for size
bb.scale(scale, scale, scale)
yt = (H - bb.YLength) / 2 - Text.TextSize.Value * 1.5
Text.Y = max(1, yt)

self.doc.recompute()
page.ViewObject.doubleClicked()
FreeCADGui.runCommand('TechDraw_ToggleFrame', 0)


class Drawing2dLegacy:
def __init__(self, scale, scale_auto, a3, cartridge, onedrawing,
drawing_name, page_name):
"""Function __init__
"""Function __init__


Line 375: Line 604:
- cartridge
- cartridge
- onedrawing
- onedrawing
v1.1 - renamed to legacy, functional wise untouched
"""
"""
self.TopX_H = 0
self.TopX_H = self.TopY_H = 0
self.TopY_H = 0
self.TopX_V = self.TopY_V = 0
self.TopX_V = 0
self.TopX_Hmax = self.TopY_Hmax = 0
self.TopY_V = 0
self.TopX_Vmax = self.TopY_Vmax = 0
self.TopX_Hmax = 0
self.TopY_Hmax = 0
self.TopX_Vmax = 0
self.TopY_Vmax = 0
self.a3 = a3
self.a3 = a3
self.pts_nbr = 100
self.scale = scale
self.scale = scale
self.scale_auto = scale_auto
self.scale_auto = scale_auto
self.cartridge = cartridge
self.cartridge = cartridge
self.onedrawing = onedrawing
self.onedrawing = onedrawing
self.marge = 6
if self.a3:
if self.a3:
self.L = 420
self.L, self.H = 420, 297
self.H = 297
self.marge = 6
else:
else:
self.L = 297
self.L, self.H = 297, 210
self.H = 210
self.marge = 6
self.page_name = page_name
self.page_name = page_name
self.drawing_name = drawing_name
self.drawing_name = drawing_name
self.doc = FreeCAD.ActiveDocument


def newPage(self):
def newPage(self):
freecad_dir = os.getenv('HOME') + '/.FreeCAD/Mod/unfoldBox'
freecad_dir = os.path.join(FreeCAD.getResourceDir(), dwgtpllegacy)
page = FreeCAD.activeDocument().addObject(
page = self.doc.addObject('Drawing::FeaturePage', self.page_name)
'Drawing::FeaturePage', self.page_name)
size = 'A3' if self.a3 else 'A4'
if self.a3:
frame = '' if self.cartridge else '_plain'
page.Template = freecad_dir + '/{}_Landscape{}.svg'.format(size, frame)
if self.cartridge:
page.Template = freecad_dir+'/A3_Landscape.svg'
else:
page.Template = freecad_dir+'/A3_Landscape_Empty.svg'
else:
if self.cartridge:
page.Template = freecad_dir+'/A4_Landscape.svg'
else:
page.Template = freecad_dir+'/A4_Landscape_Empty.svg'
return page
return page


def all_(self, objnames_l):
def all_(self, objnames_l):
obj_l = []
obj_l = []
for objid in range(objnames_l.__len__()):
for objid in range(len(objnames_l)):
if objid == 0 or not self.onedrawing:
if objid == 0 or not self.onedrawing:
self.newPage()
self.newPage()
Line 426: Line 641:


def done(self, id_, objname):
def done(self, id_, objname):
# Init.
## Init.
obj_l = []
obj_l = []
obj = objname[0]
obj, objname = objname
objname = objname[1]
bb = obj.Shape.BoundBox
xmax = obj.Shape.BoundBox.XMax-obj.Shape.BoundBox.XMin
xmax, ymax = bb.XMax - bb.XMin, bb.YMax - bb.YMin
ymax = obj.Shape.BoundBox.YMax-obj.Shape.BoundBox.YMin
if ymax > xmax:
if ymax > xmax:
Draft.rotate(obj, 90)
Draft.rotate(obj, 90)
Draft.move(obj,
bb = obj.Shape.BoundBox

FreeCAD.Base.Vector(
-obj.Shape.BoundBox.XMin, -obj.Shape.BoundBox.YMin, 0))
Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
xmax = obj.Shape.BoundBox.XMax-obj.Shape.BoundBox.XMin
bb = obj.Shape.BoundBox ## does not auto-update, have to pick up new obj
ymax = obj.Shape.BoundBox.YMax-obj.Shape.BoundBox.YMin
xmax = bb.XMax - bb.XMin
ymax = bb.YMax - bb.YMin


scale = min((self.L-4*self.marge) / xmax, (self.H-4*self.marge) / ymax)
scale = min((self.L-4*self.marge) / xmax, (self.H-4*self.marge) / ymax)
Line 446: Line 661:


if id_ == 0 or not self.onedrawing:
if id_ == 0 or not self.onedrawing:
# Init.
## Init.
FreeCAD.Console.PrintMessage('Dawing2d: init\n')
PrintMessage('Drawing2d: init\n')
self.TopX_H = self.marge*2
self.TopX_H = self.TopY_H = self.marge * 2
self.TopY_H = self.marge*2
TopX, TopY = self.TopX_H, self.TopY_H
TopX = self.TopX_H
TopY = self.TopY_H
self.TopX_H = self.TopX_H + xmax * scale + self.marge
self.TopX_H = self.TopX_H + xmax * scale + self.marge
self.TopY_H = self.TopY_H
self.TopY_H = self.TopY_H
self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
self.TopY_Hmax = max(self.TopY_Hmax, self.TopY_H + ymax*scale + self.marge)
self.TopY_Hmax = max(self.TopY_Hmax,
self.TopY_H + ymax*scale + self.marge)
self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
self.TopY_V = self.marge * 2
self.TopY_V = self.marge * 2

elif self.onedrawing:
elif self.onedrawing:
if self.TopX_H + xmax * scale < self.L:
if self.TopX_H + xmax * scale < self.L:
if self.TopY_H + ymax * scale + self.marge*2 < self.H:
if self.TopY_H + ymax * scale + self.marge*2 < self.H:
# H Add at right on same horizontal line.
## H Add at right on same horizontal line.
FreeCAD.Console.PrintMessage('Dawing2d: horizontal\n')
PrintMessage('Drawing2d: horizontal\n')
TopX = self.TopX_H
TopX, TopY = self.TopX_H, self.TopY_H
TopY = self.TopY_H
self.TopX_H = self.TopX_H + xmax * scale + self.marge
self.TopX_H = self.TopX_H + xmax * scale + self.marge
self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
self.TopY_Hmax = max(self.TopY_Hmax, self.TopY_H + ymax*scale + self.marge)
self.TopY_Hmax = max(self.TopY_Hmax,
self.TopY_H + ymax*scale + self.marge)
self.TopX_Vmax = max(self.TopX_Hmax, self.TopX_Vmax)
self.TopX_Vmax = max(self.TopX_Hmax, self.TopX_Vmax)
self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
else:
else:
#
## V Add at right on same horizontal line
# V Add at right on same horizontal line
PrintMessage('Drawing2d: vertival\n')
#
if (self.TopX_V + ymax * scale + 2 * self.marge < self.L
FreeCAD.Console.PrintMessage('Dawing2d: vertival\n')
and self.TopY_V + xmax * scale + 2*self.marge < self.H):
if self.TopX_V + ymax * scale + 2 * self.marge < self.L and self.TopY_V + xmax * scale + 2*self.marge < self.H:
Draft.rotate(obj, 90)
Draft.rotate(obj, 90)
Draft.move(obj, FreeCAD.Base.Vector(-obj.BoundBox.XMin, -obj.BoundBox.YMin, 0))
bb = obj.Shape.BoundBox
Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
TopX = self.TopX_V
TopX, TopY = self.TopX_V, self.TopY_V
TopY = self.TopY_V
self.TopX_V = self.TopX_V + ymax * scale + self.marge
self.TopX_V = self.TopX_V + ymax * scale + self.marge
self.TopY_Vmax = max(self.TopY_Vmax, self.TopY_V + xmax * scale + self.marge)
self.TopY_Vmax = max(self.TopY_Vmax,
self.TopY_V + xmax * scale + self.marge)
else:
else:
obj_l.append([obj, 'name'])
obj_l.append([obj, 'name'])
return obj_l
return obj_l
else:
else:
# H Carriage return.
## H Carriage return.
if (self.TopY_Hmax + ymax * scale + self.marge*2 < self.H):
if (self.TopY_Hmax + ymax * scale + self.marge*2 < self.H):
FreeCAD.Console.PrintMessage('Dawing2d: carriage return: '+str(self.TopY_H + ymax * scale)+' > '+str(self.H)+'\n')
msg = 'Drawing2d: carriage return: {} > {}\n'
PrintMessage(msg.format(self.TopY_H + ymax * scale, self.H))
TopX = self.marge*2
TopX = self.marge*2
TopY = self.TopY_Hmax
TopY = self.TopY_Hmax
Line 501: Line 717:
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
else:
else:
# V Add at right on same horizontal line.
## V Add at right on same horizontal line.
FreeCAD.Console.PrintMessage('Dawing2d: vertical: ' + str(self.TopX_V) + ', ' + str(self.TopX_Vmax) + '\n')
msg = 'Drawing2d: vertical: {}, {}\n'
if self.TopX_V + ymax * scale + 2*self.marge < self.L and self.TopY_V + xmax * scale + 2*self.marge < self.H :
PrintMessage(msg.format(self.TopX_V, self.TopX_Vmax))
if (self.TopX_V + ymax * scale + 2*self.marge < self.L
and self.TopY_V + xmax * scale + 2*self.marge < self.H):
Draft.rotate(obj, 90)
Draft.rotate(obj, 90)
Draft.move(obj, FreeCAD.Base.Vector(-obj.BoundBox.XMin, -obj.BoundBox.YMin, 0))
bb = obj.Shape.BoundBox
TopX = self.TopX_V
Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
TopY = self.TopY_V
TopX, TopY = self.TopX_V, self.TopY_V
self.TopX_V = self.TopX_V + ymax * scale + self.marge
self.TopX_V = self.TopX_V + ymax * scale + self.marge
self.TopY_Vmax = max(self.TopY_Vmax, self.TopY_V + xmax * scale + self.marge)
self.TopY_Vmax = max(self.TopY_Vmax,
self.TopY_V + xmax * scale + self.marge)
else:
else:
obj_l.append([obj, 'name'])
obj_l.append([obj, 'name'])
return obj_l
return obj_l


page = FreeCAD.activeDocument().getObject(self.page_name)
page = self.doc.getObject(self.page_name)


## Drawing wb (untouched)
Text = FreeCAD.activeDocument().addObject('Drawing::FeatureViewAnnotation', objname + '_txt')
Text = self.doc.addObject('Drawing::FeatureViewAnnotation', objname + '_txt')
Text.Text = objname
Text.Text = objname
Text.X = TopX + xmax / 2 * scale
Text.X = TopX + xmax / 2 * scale
Text.Y = TopY + ymax / 2 * scale
Text.Y = TopY + ymax / 2 * scale
Text.Scale = 1
Text.Scale = 7 if self.a3 else 5


TopView = FreeCAD.activeDocument().addObject('Drawing::FeatureViewPart', 'TopView')
TopView = self.doc.addObject('Drawing::FeatureViewPart', 'TopView')
TopView.Source = obj
TopView.Source = obj
TopView.Direction = (0.0, 0.0, 1)
TopView.Direction = (0.0, 0.0, 1)
Line 532: Line 752:
page.addObject(TopView)
page.addObject(TopView)
page.addObject(Text)
page.addObject(Text)

FreeCAD.activeDocument().recompute()
self.doc.recompute()
page.ViewObject.doubleClicked()

return obj_l
return obj_l




#####################################
#####################################
# Dialog Box
### Dialog Box
#####################################
#####################################
fields = [['Group Name', 'Unfolding']]
fields = [['Group Name', 'Unfolding']]
Line 552: Line 775:
fields_l.append(QtGui.QLineEdit(fields[id_][1]))
fields_l.append(QtGui.QLineEdit(fields[id_][1]))
la.addWidget(fields_l[id_])
la.addWidget(fields_l[id_])

makedwg_check = QtGui.QCheckBox(DialogBox)
makedwg_check.setObjectName('checkBox')
makedwg_check.setChecked(True)
la.addWidget(QtGui.QLabel('Make drawing'))
la.addWidget(makedwg_check)

leglay_check = QtGui.QCheckBox(DialogBox)
leglay_check.setObjectName('checkBox')
leglay_check.setChecked(False)
la.addWidget(QtGui.QLabel('Legacy layout'))
la.addWidget(leglay_check)


scale_check = QtGui.QCheckBox(DialogBox)
scale_check = QtGui.QCheckBox(DialogBox)
Line 581: Line 816:
la.addWidget(QtGui.QLabel('Sewed surfaces'))
la.addWidget(QtGui.QLabel('Sewed surfaces'))
sewed_check.setChecked(True)
sewed_check.setChecked(True)
sewed_check.setEnabled(False)
la.addWidget(sewed_check)
la.addWidget(sewed_check)


Line 594: Line 830:
QtCore.QMetaObject.connectSlotsByName(DialogBox)
QtCore.QMetaObject.connectSlotsByName(DialogBox)
DialogBox.show()
DialogBox.show()
</pre>

</nowiki>
}}

Latest revision as of 18:55, 30 July 2022

Other languages:

Sviluppa solido

Descrizione
Crea lo sviluppo di un solido con facce piane.


Versione macro: 1.0.1
Ultima modifica: 2020-03-10
Download: ToolBar Icon
Autore: Hervé B.
Autore
Hervé B.
Download
ToolBar Icon
Link
Versione macro
1.0.1
Data ultima modifica
2020-03-10
Versioni di FreeCAD
Scorciatoia
Nessuna
Vedere anche
Nessuno

Description

Descrizione

Questa macro permette di creare lo sviluppo delle superfici di un solido con facce piane, di disegnare le facce in una pagina e di inserire il disegno in un foglio A3 o A4.

Macro_unfoldBox

Installation

Installazione

Secondo quanto descritto in Macro for unfolding box surfaces

Copiare il file del codice nella directory:

  • Linux & Mac  : $home/.Freecad/Mod/unfoldBox.
  • Windows : C:\Program Files\FreeCAD0.13

Aggiungere i modelli del foglio : A3_Landscape_Empty.svg A3_Landscape.svg A4_Landscape_Empty.svg A4_Landscape.svg

Forum topic: Macro for unfolding box surfaces.

Options

Opzioni

  • Scala manuale o automatica
  • Formato della pagina: A3, A4
  • Tabella (secondo i modelli di FreeCAD)
  • Raggruppamento dei disegni nella stessa pagina se è possibile.
  • Unire le facce su un lato.

Macro_unfoldBox

Usage

Utilizzo

  1. Selezionare un oggetto creato con Part, ad es. un oggetto creato con Loft.
  2. Scomporre l'oggetto (utilizzare Draft -> Retrocedere)
  3. Selezionare le supefici
  4. Eseguire la macro

The unfolding algorithm will place the faces on the XY plane, however it seldom does the unfolding correctly. Thus some manual post-processing is needed to get to desired result, as shown in the picture below.

Macro_unfoldBox

Starting point is a box like the upper left picture.

  1. Select all faces and run the macro.
  2. Orient to Top View.
  3. Toggle the visibility on the created set of faces, the individual clones are to be visible, not the compound.
  4. Use Draft Move and Draft Rotate to reposition the unfolded cloned faces (a handful of clicks per face when snapping is used).
  5. The drawing updates automatically to the new positions.

The finished result is shown in the lower right picture above.

Script

ToolBar icon

Macro_unfoldBox.FCMacro

# -*- coding: utf-8 -*-
"""
FreeCAD Macro unfoldBox.

Unfolding of planar surfaces
"""
##################################################
# SEE https://wiki.freecadweb.org/Macro_Unfold_Box
# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2013 - DoNovae/Herve BAILLY <hbl13@donovae.com>         *
# *   Copyright (c) 2022 - heda <heda@fc-forum>                             *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__Name__ = 'Unfold Box'
__Comment__ = 'Unfolds planar surfaces and draws them on a page.'
__Author__ = 'Hervé B., heda'
__Version__ = '1.1'
__Date__ = '2022-07-28'
__License__ = 'LGPL-2.0-or-later'
__Web__ = 'https://wiki.freecad.org/Macro_Unfold_Box'
__Wiki__ = 'https://wiki.freecad.org/Macro_Unfold_Box'
__Icon__ = ''
__Help__ = ('Select surfaces, Explode them (cf Draft menu Downgrade), '
            'Select the exploded surfaces, Execute the macro')
__Status__ = ''
__Requires__ = ''
__Communication__ = ''
__Files__ = ''

__doc__ = """
select a face, or several and run the macro.
a shell/solid needs to be draft/downgraded to get the faces as separate objects

the macro is intended to unfold a set of planar faces,
in current state, most of the times it does not place the faces correctly.
thus one needs to do the final placement of the clones manually, with Draft/Move & Rotate
regardless, the macro saves a lot of clicks compared to a fully manual unfold

settings are not context aware, all settings not applicable are ignored.
as example, using autoscaling ignores the scale-value in the form

sew is always enabled, thus the drawing
is always the sewed shape on a single page


v1.1   (2022-07-28) py3/qt5 compat, cosmetic code changes,
       minor code tweaks for evolved fc-api
       added option to skip drawing, made layout engine with techdraw,
       the drawing wb layout engine is now called "legacy"
v1.0.1 (2020-03-10) - on wiki
v1.0   (2013-09-14) - on wiki

note:
- unfolding occasionally work as expected, but mostly not
- anyone is free to improve functionality

"""

import os, math

from PySide import QtGui, QtCore

import FreeCAD, FreeCADGui
import Draft

Vector = FreeCAD.Base.Vector
Units = FreeCAD.Units
PrintMessage = FreeCAD.Console.PrintMessage
PrintError = FreeCAD.Console.PrintError

fields_l = []
unroll_l = []
dwgtpllegacy = 'Mod/Drawing/Templates'
dwgtpl = 'Mod/TechDraw/Templates'


#####################################
###   Functions
#####################################


def errorDialog(msg):
    diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical, 'Error Message', msg)
    diag.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
    diag.exec_()


def proceed():
    QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)

    PrintMessage('===========================================\n')
    PrintMessage('unfoldBox: start.\n')
    try:
        file_name = fields_l[0].text()
        makedwg = makedwg_check.isChecked()
        leglay = leglay_check.isChecked()
        scale = float(fields_l[1].text())
        scale_auto = scale_check.isChecked()
        a3 = a3_check.isChecked()
        cartridge = cartridge_check.isChecked()
        onedrawing = onedrawing_check.isChecked()
        sewed = sewed_check.isChecked()
        PrintMessage('unfoldBox.file_name: {}\n'.format(file_name))
        PrintMessage('unfoldBox.makedwg: {}\n'.format(makedwg))
        PrintMessage('unfoldBox.leglay: {}\n'.format(leglay))
        PrintMessage('unfoldBox.scale: {}\n'.format(scale))
        PrintMessage('unfoldBox.scale_check: {}\n'.format(scale_auto))
        PrintMessage('unfoldBox.a3_check: {}\n'.format(a3))
        PrintMessage('unfoldBox.cartridge: {}\n'.format(cartridge))
        PrintMessage('unfoldBox.onedrawing: {}\n'.format(onedrawing))
        PrintMessage('unfoldBox.sewed: {}\n'.format(sewed))
    except:
        msg = 'unfoldBox: wrong inputs...\n'
        PrintError(msg)
        errorDialog(msg)

    QtGui.QApplication.restoreOverrideCursor()
    DialogBox.hide()

    ## Get selection
    sel = FreeCADGui.Selection.getSelection()
    if not sel or len(sel) < 2:
        PrintMessage('unfoldBox: requires at least 2 selected faces, ending.\n')
        return

    doc = FreeCAD.ActiveDocument

    objnames_l=[]
    grp = doc.addObject('App::DocumentObjectGroup', str(file_name))
    for objid in range(len(sel)):
        obj = Draft.make_clone(sel[objid])
        grp.addObject(obj)
        objnames_l.append([ obj, sel[objid].Name ])

    doc.recompute()
    unfold = unfoldBox(doc)
    if sewed:
        objnames_l = unfold.done(objnames_l)
        grp.addObject(objnames_l[0][0])
    else:
        for objid in range(len(objnames_l)):
            unfold.moveXY(objnames_l[objid][0])

    if not makedwg:
        return

    if leglay:
        idx = 0
        while len(objnames_l) > 0:
            draw = Drawing2dLegacy(scale, scale_auto, a3,
                                   cartridge, onedrawing,
                                   doc.Name, 'Page'+str(idx))
            objnames_l = draw.all_(objnames_l)
            idx += 1
            PrintMessage('unfoldBox: obj_l= {}\n'.format(len(objnames_l)))
    else:
        draw = Drawing2d(scale, scale_auto, a3, cartridge)
        draw.drawpage(objnames_l[0], "{}Page".format(file_name))

    PrintMessage('unfoldBox: end.\n')
    PrintMessage('===========================================\n')


def close():
    DialogBox.hide()


def getType(obj):
    return type(obj).__name__

def mkmm(l):
    return Units.Quantity(l, Units.Length)

class unfoldBox:
    def __init__(self, doc):
        PrintMessage('unfoldBox.unfoldBox\n')
        self.doc = doc
        self.LIMIT = 0.0001

    def done(self, objnames_l):
        tree_l = self.makeTree(objnames_l)
        for idx in range(len(objnames_l)):
            face = objnames_l[idx]
            self.moveXY(face[0])
        self.sew(objnames_l, tree_l)
        return self.fusion(objnames_l)

    def getEndPoints(self, edge):
        return [v.Point for v in edge.Vertexes]

    def makeTree(self, objnames_l):
        ## Initialisation of tree_l.
        tree_l = []
        for k in range(len(objnames_l)):
            facek = objnames_l[k][0]
            facekEdges = facek.Shape.Edges
            facek_l = []
            for i in range(len(facekEdges)):
                if False and getType(facekEdges[i].Curve) != 'GeomLineSegment':
                    ## this is a no-op...
                    facek_l.append([-1, -1])
                else:
                    ## Search face link to the ith edge
                    #vki0 = facekEdges[i].Curve.StartPoint
                    #vki1 = facekEdges[i].Curve.EndPoint
                    vki0, vki1 = self.getEndPoints(facekEdges[i])
                    found = False
                    for l in range(k+1, len(objnames_l)):
                        facel = objnames_l[l][0]
                        facelEdges = facel.Shape.Edges
                        for j in range(len(facelEdges)):
                            #vlj0 = facelEdges[j].Curve.StartPoint
                            #vlj1 = facelEdges[j].Curve.EndPoint
                            vlj0, vlj1 = self.getEndPoints(facelEdges[j])
                            if (vki0.isEqual(vlj0, self.LIMIT)
                                and vki1.isEqual(vlj1, self.LIMIT)):
                                arelinked = False
                                isfacek = isfacel = False
                                for kk in range(k-1):
                                    for ii in range(len(tree_l[kk])):
                                        isfacek = tree_l[kk][ii][0] == k
                                        isfacel = tree_l[kk][ii][0] == l
                                    if isfacek and isfacel:
                                        arelinked = True
                                        break
                                if not arelinked:
                                    facek_l.append([l, j])
                                    found = True
                                    break
                            if found:
                                break
                if not found:
                    facek_l.append([-1, -1])
            tree_l.append(facek_l)
        return tree_l

    def sew(self, objnames_l, tree_l):
        placed_l = []
        for k in range(len(tree_l)):
            iskplaced = False
            for p in range(len(placed_l)):
                iskplaced = placed_l[p] == k
            if not iskplaced:
                placed_l.append(k)
            facek = tree_l[k]
            objk = objnames_l[k][0]
            for i in range(len(facek)):
                edgeki = facek[i]
                l, j = edgeki[:2]
                islplaced = False
                for p in range(len(placed_l)):
                    if placed_l[p] == l:
                        islplaced = True
                        break
                if not islplaced:
                    placed_l.append(l)
                if l >= 0 and not (islplaced and iskplaced):
                    iskplaced = True
                    ## Move facel.edgelj to facek.edgeki.
                    objl = objnames_l[l][0]
                    #vki0 = objk.Shape.Edges[i].Curve.StartPoint
                    #vki1 = objk.Shape.Edges[i].Curve.EndPoint
                    vki0, vki1 = self.getEndPoints(objk.Shape.Edges[i])
                    #vlj0 = objl.Shape.Edges[j].Curve.StartPoint
                    #vlj1 = objl.Shape.Edges[j].Curve.EndPoint
                    vlj0, vlj1 = self.getEndPoints(objk.Shape.Edges[j])
                    vk = vki1.sub(vki0)
                    vl = vlj1.sub(vlj0)
                    alpk = vk.getAngle(vl) * 180 / math.pi
                    alpl = vl.getAngle(vk) * 180 / math.pi
                    self.isPlanZ(objk)
                    if islplaced:
                        Draft.move(objk, vlj0.sub(vki0))
                    else:
                        Draft.move(objl, vki0.sub(vlj0))
                    self.isPlanZ(objk)

                    if math.fabs(vk.dot(Vector(-vl.y, vl.x, 0))) > self.LIMIT:
                        if islplaced:
                            Draft.rotate(objk, -alpl, vlj0, self.vecto(vl, vk))
                        else:
                            Draft.rotate(objl, -alpk, vki0, self.vecto(vk, vl))
                    elif vk.dot(vl) < 0:
                        if islplaced:
                            Draft.rotate(objk, 180, vlj0, self.vecto(vl, Vector(-vl.y, vl.x, 0)))
                        else:
                            Draft.rotate(objl, 180, vki0, self.vecto(vk, Vector(-vk.y, vk.x, 0)))
                    ## Verifications.
                    #vki0 = objk.Shape.Edges[i].Curve.StartPoint
                    #vki1 = objk.Shape.Edges[i].Curve.EndPoint
                    vki0, vki1 = self.getEndPoints(objk.Shape.Edges[i])
                    #vlj0 = objl.Shape.Edges[j].Curve.StartPoint
                    #vlj1 = objl.Shape.Edges[j].Curve.EndPoint
                    vli0, vli1 = self.getEndPoints(objk.Shape.Edges[j])
                    vk = vki1.sub(vki0)
                    vl = vlj1.sub(vlj0)
                    self.isPlanZ(objk)

                    ## Flip or not.
                    bbl, bbk = objl.Shape.BoundBox, objk.Shape.BoundBox
                    L = max(bbl.XMax, bbk.XMax) - min(bbl.XMin, bbk.XMin)
                    W = max(bbl.YMax, bbk.YMax) - min(bbl.YMin, bbk.YMin)
                    S1 = L * W
                    if islplaced:
                        Draft.rotate(objk, 180, vlj0, vl)
                    else:
                        Draft.rotate(objl, 180, vki0, vk)
                    bbl, bbk = objl.Shape.BoundBox, objk.Shape.BoundBox
                    L = max(bbl.XMax, bbk.XMax) - min(bbl.XMin, bbk.XMin)
                    W = max(bbl.YMax, bbk.YMax) - min(bbl.YMin, bbk.YMin)
                    S2 = L * W
                    if S2 <= S1:
                        if islplaced:
                            Draft.rotate(objk, 180, vlj0, vl)
                        else:
                            Draft.rotate(objl, 180, vki0, vk)
                    self.isPlanZ(objk)

    def isPlanZ(self, obj):
        bb = obj.Shape.BoundBox
        L = bb.XMax - bb.XMin
        W = bb.YMax - bb.YMin
        H = bb.ZMax - bb.ZMin
        return H < self.LIMIT


    def fusion(self, objnames_l):
        ## Init.
        obj_l, objna_l =[], []
        obj0, name = objnames_l[0]
        objfuse = self.doc.addObject('Part::MultiFuse', 'Unfolding')
        for k in range(len(objnames_l)):
            objk = objnames_l[k][0]
            obj_l.append(objk)

        objfuse.Shapes = obj_l
        self.doc.recompute()
        objna_l.append([objfuse, name])
        return objna_l

    def get2Vectors(self, shape):
        """not used in v1.1"""
        v0, v1 = Vector(), Vector()

        edges = shape.Edges
        for idx in range(len(edges) - 1):
            e1, e2 = edges[idx], edges[idx + 1]
            ## .EndPoint errors out...
            va = e1.Curve.EndPoint.sub(e1.Curve.StartPoint)
            vb = e2.Curve.EndPoint.sub(e2.Curve.StartPoint)
            if vb.sub(va).Length > v1.sub(v0).Length:
                v0, v1 = self.vect_copy(va), self.vect_copy(vb)
        return [v0, v1]

    def vecto(self, vect1, vect2):
        '''Function vecto.'''
        v = Vector()
        v.x = vect1.y * vect2.z - vect1.z * vect2.y
        v.y = vect1.z * vect2.x - vect1.x * vect2.z
        v.z = vect1.x * vect2.y - vect1.y * vect2.x
        return v

    def vect_copy(self, vect):
        '''Return a copy of vector.'''
        return vect.add(Vector())

    def moveXY(self, obj):
        ## Move to origin
        bb = obj.Shape.BoundBox
        Draft.move(obj, Vector(-bb.XMin, -bb.YMin, -bb.ZMin))

        ## Find 2 vectors defining the plan of surface
        #tab = self.get2Vectors(obj.Shape)
        #v0, v1 = tab[:2]
        #norm = self.vecto(v0, v1)
        #norm.normalize()
        ## above gives null vector, and errors out...

        ## probably sprung out of some api-change over time
        ## probably part.line vs part.linesegment...
        ## a part.line does not have edge.Curve.StartPoint, or .EndPoint any more
        ## the semantics is to get first & second point from a line,
        ## so edge.Curve.value(edge.Curve.FirstParameter) should get the sought values
        ## however easier to just use edge.Vertexes...
        ## which appears to have same orientation

        norm = obj.Shape.findPlane().Axis ## works for planar surfaces
        #norm = obj.Shape.Surface.Axis ## gives worse results...
        norm.normalize()

        ## Rotate.
        nx, ny, nz = (math.fabs(xyz) for xyz in norm)
        if nx < self.LIMIT and nz < self.LIMIT:
            Draft.rotate(obj, 90, Vector(0, 0, 0), Vector(1, 0, 0))
        elif ny < self.LIMIT and nz < self.LIMIT:
            Draft.rotate(obj, 90, Vector(0, 0, 0), Vector(0, 1, 0))
        else:
            ## Rotate following the angle to the normal direction of the plan.
            oz = Vector(0, 0, 1)
            alp = oz.getAngle(norm) * 180 / math.pi
            vecto = self.vecto(oz, norm)
            rotv = oz if vecto.isEqual(Vector(), self.LIMIT) else self.vecto(oz, norm)
            #rotv = norm if vecto.isEqual(Vector(), self.LIMIT) else self.vecto(oz, norm)
            ## flipping here seems to give worse results as well...
            Draft.rotate(obj, -alp, Vector(0, 0, 0), rotv)

        ## Move to z = 0.
        Draft.move(obj, Vector(0, 0, -obj.Shape.BoundBox.ZMin))


class Scale:
    """keeps autoscaling to integers"""
    def __init__(self, scale):
        self.scale = scale if scale >= 1 else 1 / scale
        self.scale = int(self.scale)
        self.inverted = scale >= 1

    def get(self):
        if self.inverted:
            return self.scale, '{}:1'.format(self.scale)
        else:
            return 1/self.scale, '1:{}'.format(self.scale)


class Drawing2d:
    def __init__(self, scale, scale_auto, a3, cartridge):
        """techdraw based layout for one page only"""
        self.scale = scale
        self.scale_auto = scale_auto
        self.a3 = a3
        self.cartridge = cartridge
        if a3:
            self.WH = 420, 297
        else:
            self.WH = 297, 210
        self.doc = FreeCAD.ActiveDocument

    def newPage(self, page_name):
        freecad_dir = os.path.join(FreeCAD.getResourceDir(), dwgtpl)
        page = self.doc.addObject('TechDraw::DrawPage', page_name)
        template = self.doc.addObject('TechDraw::DrawSVGTemplate', 'Template')
        size = 'A3' if self.a3 else 'A4'
        frame = 'TD' if self.cartridge else '_blank'
        template.Template = freecad_dir + '/{}_Landscape{}.svg'.format(size, frame)
        page.Template = template

        return page

    def drawpage(self, objname, page_name):
        page = self.newPage(page_name)
        faceset, name = objname

        bb = faceset.Shape.BoundBox
        ## bb does not auto-update, have to pick up new bb after manipulation
        if bb.YLength > bb.XLength: ## auto rotate
            Draft.rotate(faceset, 90)
            bb = faceset.Shape.BoundBox

        Draft.move(faceset, Vector(-bb.XMin, -bb.YMin, 0))
        bb = faceset.Shape.BoundBox

        W, H = self.WH
        xr, yr = W / bb.XLength, H / bb.YLength
        adjust = 0.7 if self.cartridge else 0.85
        scale = min(xr, yr) * adjust if self.scale_auto else self.scale
        scale, scr = Scale(scale).get()
        bb.scale(scale, scale, 1) ## faceset in xy-plane


        TopView = self.doc.addObject('TechDraw::DrawViewPart', 'TopView')
        page.addView(TopView)
        TopView.Source = faceset
        TopView.Direction = (0, 0, 1)
        TopView.XDirection = (1, 0, 0)
        TopView.Scale = scale

        Text = self.doc.addObject('TechDraw::DrawViewAnnotation', name)
        page.addView(Text)
        Text.Text = name
        Text.recompute() # for size
        bb.scale(scale, scale, scale)
        yt = (H - bb.YLength) / 2 - Text.TextSize.Value * 1.5
        Text.Y = max(1, yt)

        self.doc.recompute()
        page.ViewObject.doubleClicked()
        FreeCADGui.runCommand('TechDraw_ToggleFrame', 0)


class Drawing2dLegacy:
    def __init__(self, scale, scale_auto, a3, cartridge, onedrawing,
                 drawing_name, page_name):
        """Function __init__

        - scale
        - scale_auto
        - a3
        - cartridge
        - onedrawing
        v1.1 - renamed to legacy, functional wise untouched
        """
        self.TopX_H = self.TopY_H = 0
        self.TopX_V = self.TopY_V = 0
        self.TopX_Hmax = self.TopY_Hmax = 0
        self.TopX_Vmax = self.TopY_Vmax = 0
        self.a3 = a3
        self.scale = scale
        self.scale_auto = scale_auto
        self.cartridge = cartridge
        self.onedrawing = onedrawing
        self.marge = 6
        if self.a3:
            self.L, self.H = 420, 297
        else:
            self.L, self.H = 297, 210
        self.page_name = page_name
        self.drawing_name = drawing_name
        self.doc = FreeCAD.ActiveDocument

    def newPage(self):
        freecad_dir = os.path.join(FreeCAD.getResourceDir(), dwgtpllegacy)
        page = self.doc.addObject('Drawing::FeaturePage', self.page_name)
        size = 'A3' if self.a3 else 'A4'
        frame = '' if self.cartridge else '_plain'
        page.Template = freecad_dir + '/{}_Landscape{}.svg'.format(size, frame)
        return page

    def all_(self, objnames_l):
        obj_l = []
        for objid in range(len(objnames_l)):
            if objid == 0 or not self.onedrawing:
                self.newPage()
            obj_l.extend(self.done(objid, objnames_l[objid]))
        return obj_l

    def done(self, id_, objname):
        ## Init.
        obj_l = []
        obj, objname = objname
        bb = obj.Shape.BoundBox
        xmax, ymax = bb.XMax - bb.XMin, bb.YMax - bb.YMin
        if ymax > xmax:
            Draft.rotate(obj, 90)
            bb = obj.Shape.BoundBox

        Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
        bb = obj.Shape.BoundBox ## does not auto-update, have to pick up new obj
        xmax = bb.XMax - bb.XMin
        ymax = bb.YMax - bb.YMin

        scale = min((self.L-4*self.marge) / xmax, (self.H-4*self.marge) / ymax)

        if (not self.scale_auto) or (self.onedrawing):
            scale = self.scale

        if id_ == 0 or not self.onedrawing:
            ## Init.
            PrintMessage('Drawing2d: init\n')
            self.TopX_H = self.TopY_H = self.marge * 2
            TopX, TopY = self.TopX_H, self.TopY_H
            self.TopX_H = self.TopX_H + xmax * scale + self.marge
            self.TopY_H = self.TopY_H
            self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
            self.TopY_Hmax = max(self.TopY_Hmax,
                                 self.TopY_H + ymax*scale + self.marge)
            self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
            self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
            self.TopY_V = self.marge * 2

        elif self.onedrawing:
            if self.TopX_H + xmax * scale < self.L:
                if self.TopY_H + ymax * scale + self.marge*2 < self.H:
                    ## H Add at right on same horizontal line.
                    PrintMessage('Drawing2d: horizontal\n')
                    TopX, TopY = self.TopX_H, self.TopY_H
                    self.TopX_H = self.TopX_H + xmax * scale + self.marge
                    self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
                    self.TopY_Hmax = max(self.TopY_Hmax,
                                         self.TopY_H + ymax*scale + self.marge)
                    self.TopX_Vmax = max(self.TopX_Hmax, self.TopX_Vmax)
                    self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
                    self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
                else:
                    ## V Add at right on same horizontal line
                    PrintMessage('Drawing2d: vertival\n')
                    if (self.TopX_V + ymax * scale + 2 * self.marge < self.L
                        and self.TopY_V + xmax * scale + 2*self.marge < self.H):
                        Draft.rotate(obj, 90)
                        bb = obj.Shape.BoundBox
                        Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
                        self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
                        TopX, TopY = self.TopX_V, self.TopY_V
                        self.TopX_V = self.TopX_V + ymax * scale + self.marge
                        self.TopY_Vmax = max(self.TopY_Vmax,
                                             self.TopY_V + xmax * scale + self.marge)
                    else:
                        obj_l.append([obj, 'name'])
                        return obj_l
            else:
                ## H Carriage return.
                if (self.TopY_Hmax + ymax * scale + self.marge*2 < self.H):
                    msg = 'Drawing2d: carriage return: {} > {}\n'
                    PrintMessage(msg.format(self.TopY_H + ymax * scale, self.H))
                    TopX = self.marge*2
                    TopY = self.TopY_Hmax
                    self.TopX_H = TopX + xmax * scale + self.marge
                    self.TopY_H = TopY
                    self.TopX_Hmax = max(self.TopX_Hmax, self.TopX_H)
                    self.TopY_Hmax = self.TopY_Hmax + ymax*scale+self.marge
                    self.TopX_Vmax = max(self.TopX_Vmax, self.TopX_Hmax)
                    self.TopX_V = max(self.TopX_Vmax, self.TopX_V)
                else:
                    ## V Add at right on same horizontal line.
                    msg = 'Drawing2d: vertical: {}, {}\n'
                    PrintMessage(msg.format(self.TopX_V, self.TopX_Vmax))
                    if (self.TopX_V + ymax * scale + 2*self.marge < self.L
                        and self.TopY_V + xmax * scale + 2*self.marge < self.H):
                        Draft.rotate(obj, 90)
                        bb = obj.Shape.BoundBox
                        Draft.move(obj, Vector(-bb.XMin, -bb.YMin, 0))
                        TopX, TopY = self.TopX_V, self.TopY_V
                        self.TopX_V = self.TopX_V + ymax * scale + self.marge
                        self.TopY_Vmax = max(self.TopY_Vmax,
                                             self.TopY_V + xmax * scale + self.marge)
                    else:
                        obj_l.append([obj, 'name'])
                        return obj_l

        page = self.doc.getObject(self.page_name)

        ## Drawing wb (untouched)
        Text = self.doc.addObject('Drawing::FeatureViewAnnotation', objname + '_txt')
        Text.Text = objname
        Text.X = TopX + xmax / 2 * scale
        Text.Y = TopY + ymax / 2 * scale
        Text.Scale = 7 if self.a3 else 5

        TopView = self.doc.addObject('Drawing::FeatureViewPart', 'TopView')
        TopView.Source = obj
        TopView.Direction = (0.0, 0.0, 1)
        TopView.Rotation = 0
        TopView.X = TopX
        TopView.Y = TopY
        TopView.ShowHiddenLines = True
        TopView.Scale = scale
        page.addObject(TopView)
        page.addObject(Text)

        self.doc.recompute()
        page.ViewObject.doubleClicked()

        return obj_l


#####################################
###   Dialog Box
#####################################
fields = [['Group Name', 'Unfolding']]
fields.append(['Scale', '1'])

DialogBox = QtGui.QDialog()
DialogBox.resize(250, 250)
DialogBox.setWindowTitle('unfoldBox')
la = QtGui.QVBoxLayout(DialogBox)

# Input fields.
for id_ in range(len(fields)):
    la.addWidget(QtGui.QLabel(fields[id_][0]))
    fields_l.append(QtGui.QLineEdit(fields[id_][1]))
    la.addWidget(fields_l[id_])

makedwg_check = QtGui.QCheckBox(DialogBox)
makedwg_check.setObjectName('checkBox')
makedwg_check.setChecked(True)
la.addWidget(QtGui.QLabel('Make drawing'))
la.addWidget(makedwg_check)

leglay_check = QtGui.QCheckBox(DialogBox)
leglay_check.setObjectName('checkBox')
leglay_check.setChecked(False)
la.addWidget(QtGui.QLabel('Legacy layout'))
la.addWidget(leglay_check)

scale_check = QtGui.QCheckBox(DialogBox)
scale_check.setObjectName('checkBox')
scale_check.setChecked(True)
la.addWidget(QtGui.QLabel('Scale auto'))
la.addWidget(scale_check)

a3_check = QtGui.QCheckBox(DialogBox)
a3_check.setObjectName('checkBox')
la.addWidget(QtGui.QLabel('A3 Format'))
a3_check.setChecked(False)
la.addWidget(a3_check)

cartridge_check = QtGui.QCheckBox(DialogBox)
cartridge_check.setObjectName('checkBox')
la.addWidget(QtGui.QLabel('Cartridge'))
cartridge_check.setChecked(False)
la.addWidget(cartridge_check)

onedrawing_check = QtGui.QCheckBox(DialogBox)
onedrawing_check.setObjectName('checkBox')
la.addWidget(QtGui.QLabel('Group drawings in page'))
onedrawing_check.setChecked(True)
la.addWidget(onedrawing_check)

sewed_check = QtGui.QCheckBox(DialogBox)
sewed_check.setObjectName('checkBox')
la.addWidget(QtGui.QLabel('Sewed surfaces'))
sewed_check.setChecked(True)
sewed_check.setEnabled(False)
la.addWidget(sewed_check)

box = QtGui.QDialogButtonBox(DialogBox)

box = QtGui.QDialogButtonBox(DialogBox)
box.setOrientation(QtCore.Qt.Horizontal)
box.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
la.addWidget(box)

QtCore.QObject.connect(box, QtCore.SIGNAL('accepted()'), proceed)
QtCore.QObject.connect(box, QtCore.SIGNAL('rejected()'), close)
QtCore.QMetaObject.connectSlotsByName(DialogBox)
DialogBox.show()