DXF Export using TechDraw classes

Discussions about the development of the TechDraw workbench
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

DXF Export using TechDraw classes

Postby petit_chat_noir » Fri Nov 08, 2019 3:51 pm

Hello,
It could be very useful if techdraw were able to copy Edge/Lines colors from the 3Dview to retreive it in generated views, and also set this color when you do a dxf export ! :D
The best should be to detect Draft.Layers and create them in the view->dxf export
It is very important when you do architecture/building plans, to separate parts by layers or colors.
In other way,, TechDraw could provide python functions to retreive an array of hidden lines coordinates, and one array of lines by Shapes converted
I tried to read the cpp code from TechDraw but I'm unable to do that, it's too hard for me.

French:
Cela pourrait être très utile que TechDraw différencie les couleurs de pièces attribuées dans la vue 3D, et que ces couleur soient exportées au moment de l'export dxf d'une vue.
Le top serait que techDraw arrive à détecter les Layers créé depuis Draft, et qu'il les appliquent au moment de l'export dxf tout en séparant les lignes cachées dans un autre layer...

Ou alors, si TechDraw pouvait avoir des fonctions python d'accès à un tableau contenant les lignes cachées générés, et un tableau de lignes par objets générés, on pourrait faire des merveilles ! on pourrait avec ces fonctions sur un export dxf séparer les éléments dans des calques distincts par exemple ! 8-)
Je n'ai rien trouvé à ce sujet

pourquoi cette requette : Dans mon domaine, j'utilise des cotes cumulés par exemple, pour la fabrication de murs ossature bois. Je suis donc obligé de passer par un export dxf pour travailler mes plans. J'ai des cotes d'altitudes, et j'en passe. Je travaille beaucoup avec les épaisseurs de traits, et sans export dans des calques séparés c'est impossible

thanks !
Last edited by petit_chat_noir on Sat Nov 16, 2019 3:35 pm, edited 1 time in total.
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

Re: TechDraw feature request

Postby petit_chat_noir » Sat Nov 09, 2019 10:21 am

Hello I've made some screenshots :
Here is my 3D view :
what I have.png
what I have.png (72.1 KiB) Viewed 614 times
if I generate view I have good hidden lines job ! but no line color :
What it produce.png
What it produce.png (70.31 KiB) Viewed 614 times
Finally if I export to dxf, this is what I have, only one layer called "view", no hidden lines separation:
view to dxf.png
view to dxf.png (63.17 KiB) Viewed 614 times
And know I've made a script with dxf layer creation :

Code: Select all

import ezdxf
from ezdxf.tools.standards import linetypes
import FreeCADGui,DraftLayer
import Drawing
import TechDraw
from pivy import coin


#Select all objects
for sel in FreeCAD.ActiveDocument.Objects:
	if sel.ViewObject.isVisible():
		FreeCADGui.Selection.addSelection(sel)

obj= FreeCADGui.Selection.getSelection()

# Detect Draft Layers in selection
layers=[]
for i in obj:
	#print( i)
	if hasattr(i,'Proxy'):
		if type(i.Proxy) ==DraftLayer.Layer:
			layers.append(i)


#DXF CREATION
doc = ezdxf.new(dxfversion='R2010')

####Create layers in the DXF
for i in layers:
# Create new table entries (layers, linetypes, text styles, ...).
	doc.layers.new(i.Label, dxfattribs={'color': 2})
	l=doc.layers.get(i.Label)
	col=i.ViewObject.ShapeColor
	#Conversion float to RGB
	r=col[0]*255
	g=col[1]*255
	b=col[2]*255
	l.rgb =(r,g,b)

##add hidden lines Layer
#add hidden line style
doc.linetypes.new('HIDDEN', dxfattribs={'description': "Hidden _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ", 'pattern': 'A,6.35,-3.175'})
txt_hidden="Hidden lines"
doc.layers.new(txt_hidden, dxfattribs={'color': 3})
l=doc.layers.get(txt_hidden)
l.dxf.linetype = 'HIDDEN'

####TODO
#### copier les éléments et les remettre sur un plan XY
#### analyser qui est devant et qui est derrière
#### séparer les lignes lorsqu'ellessont à moitité caché (chaud)


### start modelspace
msp = doc.modelspace()
### Now try to extract lines
#select shapes in layer

doc.layers.new("TESTS", dxfattribs={'color':13})

for lay in layers:
	print("Select shapes in layer:",lay.Label,"\n")
	group=lay.Group
	for part in group :
		if hasattr(part,"Shape") :
			for l in part.Shape.Edges:
				#get vertices of edges
				start=l.Vertexes[0]	
				end=l.Vertexes[1]	
				msp.add_line((start.X,start.Y,start.Z),(end.X,end.Y,end.Z),dxfattribs={'layer':lay.Label})
doc.saveas('test.dxf')
this is what it generate, all layers are created, but I'm unable to calculate and separate hidden lines... :
script generation.png
script generation.png (75.65 KiB) Viewed 614 times
Édit: I think i have to work with viewobject, and not Shapes object....
User avatar
wandererfan
Posts: 3325
Joined: Tue Nov 06, 2012 5:42 pm

Re: TechDraw feature request

Postby wandererfan » Sat Nov 09, 2019 2:31 pm

Hidden lines are calculated by the OCC Hidden Line Removal (HLR) algorithms. Once a shape passes through HLR any connection between Edges in the source and Edges in the result is lost. So there is no way of knowing what appearance attributes in source belong with which object in the result. In addition, the face detection logic has to break some Edges into pieces.

There are special cases where this is possible - ex Sketches don't need to have hidden lines removed so there is a 1:1 correspondence between source geometry and result geometry and Sketcher appearance attributes could be carried over to the drawing.
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

Re: TechDraw feature request

Postby petit_chat_noir » Sat Nov 09, 2019 3:03 pm

So i think there's a way to make it up.
First : I would like to retreive shape's lines in 2d (exactly has shown in 3dview), techDraw does, so why not me ? :D

retreive once with shaded ( hidden line not shown) HLR ?
retreive twice with all lines .

try to find intersections between hidden and not hidden -> remove doubles -> separate hidden lines -> and to finish separate visible lines in different layers.

I'm unable to retreive 2d coordinates of shown shapes like teckDraw does.
Where I Have to find ? in Gui.ViewProvider ? in coin ?
Is HLR accesible in python ?
Do you think it's possible to combine HLR, with another extraction method and compare/merge the two results ??
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

Re: TechDraw feature request

Postby petit_chat_noir » Sat Nov 09, 2019 4:37 pm

Ok,
wandererfan(thanks):I probably know how hidden lines are calculated. It use OpenCasCade, yes ?. And now, How do you pass Shape in OCC and retreive result in python ?
My way should be(probably) to pass, shape by shape and compare the result with all shapes passed with HLR algo.
I think it's not impossible, we just have to send more OCC calculations and merge/compare them.
I just want functions to export my shapes viewed in 3D in OCC functions/doc to start my tests.
I don't know how to do .... it's frustrating
User avatar
yorik
Site Admin
Posts: 11614
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: TechDraw feature request

Postby yorik » Mon Nov 11, 2019 9:15 pm

Note that with recent 0.19 versions, if you use the Draft view, it now has an option to keep original line width/colors/style. This gives pretty faithful autocad-like sheets...

But Draft views don't work for 3D of course... One solution for 3D is to use the Coin view feature of Arch views. Not sure line colors are working though... Needs some testing
User avatar
Kunda1
Posts: 6213
Joined: Thu Jan 05, 2017 9:03 pm

Re: TechDraw feature request

Postby Kunda1 » Wed Nov 13, 2019 11:54 am

we need better documentation for this. @petit_chat_noir do you mind jotting some down when you find the solution ?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
wandererfan
Posts: 3325
Joined: Tue Nov 06, 2012 5:42 pm

Re: TechDraw feature request

Postby wandererfan » Wed Nov 13, 2019 2:43 pm

petit_chat_noir wrote:
Sat Nov 09, 2019 4:37 pm
My way should be(probably) to pass, shape by shape and compare the result with all shapes passed with HLR algo.
I think it's not impossible, we just have to send more OCC calculations and merge/compare them.
I just want functions to export my shapes viewed in 3D in OCC functions/doc to start my tests.
I don't know how to do .... it's frustrating
After git commit 0c99d32dd4 there are 2 new python functions that might interest you.

Code: Select all

import TechDraw
dvp = App.ActiveDocument.View
vizEdgeList = dvp.getVisibleEdges()     #result is list of Part::TopoShapeEdge
hidEdgeList = dvp.getHiddenEdges()
The result is projection onto XY plane.
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

Re: TechDraw feature request

Postby petit_chat_noir » Wed Nov 13, 2019 6:50 pm

yes ! awesome work @wandererfan !!
It works perfeclty
dxf with hidden separated.png
dxf with hidden separated.png (6.41 KiB) Viewed 330 times
Now I have to separate visible lines by layers and it's done
User avatar
petit_chat_noir
Posts: 69
Joined: Wed Oct 02, 2019 6:45 pm

Re: TechDraw feature request

Postby petit_chat_noir » Wed Nov 13, 2019 9:47 pm

wandererfan wrote:
Wed Nov 13, 2019 2:43 pm

After git commit 0c99d32dd4 there are 2 new python functions that might interest you.

The result is projection onto XY plane.
arf,
Just a small problem. if I Extract Layer by layer, the projections haven't got the same coordinates
Layer by layer.png
Layer by layer.png (9.46 KiB) Viewed 309 times
in red : all the model
in green dashed : hidden lines
in other colors, the layer separation

do I have to pass an argument to the TechDraw.View object ?
this seems that the view is centered on the selected objects. I think that if I create a bounding box(a container) of my render, all my layers will have the same coordinates, isn't it ?