Open andreasplesch opened 4 years ago
try this
import os
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.TCollection import TCollection_ExtendedString
from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.VrmlAPI import VrmlAPI_Writer
stp_filename = os.path.join('..', 'assets', 'models', 'as1_pe_203.stp')
# load the STEP file
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
step_reader = STEPCAFControl_Reader()
step_reader.SetColorMode(True)
step_reader.SetLayerMode(True)
step_reader.SetNameMode(True)
step_reader.SetMatMode(True)
step_reader.SetGDTMode(True)
status = step_reader.ReadFile(stp_filename)
if status == IFSelect_RetDone:
step_reader.Transfer(doc)
else:
raise IOError("STEP file could not be read.")
# export the document
vrml_exporter = VrmlAPI_Writer()
vrml_exporter.WriteDoc(doc, "vrml_ocaf_stp_export.vrml", 1.)
thanks. Hm, does not load anything. Perhaps there is something which needs to be done with the document.
Is the vrml file generated ?
Yes, it is but without geometry:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile="Interchange" version="3.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:noNamespaceSchemaLocation="http://www.web3d.org/specifications/x3d-3.0.xsd">
<head>
<meta name="generator" content="tovrmlx3d, http://castle-engine.sourceforge.net/view3dscene.php#section_converting" />
<meta name="source" content="as1_pe_203.wrl" />
</head>
<Scene>
<WorldInfo
info='"Generated by Open CASCADE Technology 7.4"' />
<Group DEF="AS1_PE_ASM">
<Transform />
<Transform
translation="889 1.3042347150188371E-12 0"
rotation="0 -1 0 1.5707963705062866">
<Transform />
<Transform
translation="0 254 508"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
<Transform
translation="329.9556884765625 254 1079.5"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
<Transform
translation="-329.9556884765625 254 1079.5"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
</Transform>
<Transform
translation="-3429 0 0"
rotation="0 1 0 1.5707963705062866">
<Transform />
<Transform
translation="0 254 508"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
<Transform
translation="329.9556884765625 254 1079.5"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
<Transform
translation="-329.9556884765625 254 1079.5"
rotation="0 0 1 3.1415927410125732">
<Transform />
<Transform
translation="0 838.20001220703125 0" />
</Transform>
</Transform>
<Transform
translation="1270 1016 1.4297229849677406E-12"
rotation="0 0.70710676908493042 0.70710676908493042 3.1415927410125732">
<Transform />
<Transform
translation="4699 0 0"
rotation="0 0 1 1.5707963705062866" />
<Transform
translation="381 0 0"
rotation="0 0 -1 1.5707963705062866" />
</Transform>
</Group>
</Scene>
</X3D>
I am trying to reproduce what writeDoc does.
It recognizes the assembly. The first label is a reference. The reference is to another assembly.
The first label of that is a reference. That reference is to a SimpleShape.
is how a SimpleShade is added as a vrml node.
It looks like if there are onyly edges and no faces, it does not generate geometry.
I will try another example.
Have a look at @dblanding 's project https://github.com/dblanding/kodacad He uses pythonocc/ocaf to load and display a STEP file and its structure.
btw, the as1_pe_203.stp
file does contain 3d geometry, not only edges. There are 2 (maybe more) free edges at root, which confuses IMO the vrml exporter. Indeed you can try with another step file.
screenshots/doc at https://dblanding.github.io/kodacad/
I am still hoping that it possible to massage the document so that writeDoc starts to work.
Now, it seems to come down to essentially debugging addShape:
AddShape works for Shape to Vrml but addShape does not work for Doc to Vrml. Both use makeTShapeNode(), so we know that makeTShapeNode should work. addShape for doc checks more for isNull names, perhaps that is the issue.
is the commit which introduced writeDoc
is how the occ example reads step.
there is a transfer of multiple roots.
But this does not use a document.
That's basically the python code I posted above
Exactly. I also added
from OCC.Core.VrmlAPI import VrmlAPI_Writer, VrmlAPI_ShadedRepresentation
...
step_reader.SetPropsMode(True)
...
vrml_exporter.SetRepresentation(VrmlAPI_ShadedRepresentation)
But this did not change anything.
Also, I tried VentilatorAP203, as1_pe_203, as1-oc-214, 11752 and screw step files but none worked.
XDEDRAW 7.4.0 also uses the new writeDoc to save to VRML. Do you have a compiled version for it ? For testing ?
http://git.dev.opencascade.org/gitweb/?p=occt.git&a=search&h=HEAD&st=commit&s=VRML has a few bug reports after the writeDoc commit. These may be related. But should be all fixed in 7.4.0
I installed the opencascade package which includes precompiled draw which has XDEDRAW.
I could use tcl ReadStep and WriteVrml commands with draw.
But this also generates vrml without data. The same result.
Perhaps time to ask on the forum.
Hm, WriteVrml with Draw did work, but only after displaying the document in the viewer with XDisplay.
Draw[25]> pwd
C:/OpenCASCADE-7.4.0-vc14-64/opencascade-7.4.0/data/step
Draw[26]> vclear
Draw[27]> vinit View1
Driver1/Viewer1/View1
Draw[28]> ReadStep D screw.step
File STEP to read : screw.step
Document saved with name D
Draw[29]> XDisplay -dispMode 1 D
D:the product name
Draw[30]> vfit
Draw[31]> WriteVrml D screw.wrl
I asked on the forum:
https://www.opencascade.com/content/writevrml-works-only-after-xdisplay
can you please post here the reply (if ever you have one) ?
Will do. But it is doubtful that there would be a response.
There must be some initialization for the rendering of the doc which then helps with writeDoc .
I think addShape uses some presentation methods.
When trying to reproduce what XShow does, I noticed that TPrsStd_AISPresentation does not seem to have pythonocc binding.
It also works to write out a step file before the vrml.Unfortunately, I did not look closely enough. This does not work.
Draw[8]> ReadStep A screw.step
File STEP to read : screw.step
Document saved with name A
Draw[9]> WriteStep A screwout.step
Translating document A to STEP
*******************************************************************
****** Statistics on Transfer (Write) ******
*******************************************************************
****** Transfer Mode = 0 I.E. As Is ******
****** Transferring Shape, ShapeType = 2 ******
Writing STEP file screwout.step
** WorkSession : Sending all data
Step File Name : screwout.step(1243 ents) Write Done
File screwout.step written
Draw[10]> WriteVrml A screwout.wrl
There was a response. The recommendation is to use incmesh to triangulate the shape(s). incmesh uses BRepMesh_IncrementalMesh Is that what the Tesselator uses ?
Yes, the BRepMesh_IncrementalMesh
class, see https://github.com/tpaviot/pythonocc-core/blob/master/src/Tesselator/ShapeTesselator.cpp#L99
VrmlAPI.writeDoc() works now by using BRepMesh_IncrementalMesh
. See first part here:
https://mybinder.org/v2/gh/andreasplesch/OCCToX3D/conda2?filepath=notebooks/doc_to_wire_vrml.ipynb
VrmlAPI.writeDoc() does not support edge output. I tried many things but it seems it is necessary to explore the full tree. Just extracting the edges from the root shape does not include all the edges in referred shapes. I may ask on the forum, how to do edge output with VrmlAPI.
In the end, it was perhaps easiest to redo the x3d export with X3DExporter on all the shapes which with_names_colors provides but then only use the linesets, after combining in XML. This is the second part above.
https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_vrml_a_p_i___writer.html#a1ef8a5a09453744e5e7bbea4d4a2eb28
It should deal with assemblies, better labels, and perhaps colors.
Look at read_step_file_names_colors for how to deal with documents (OCAF).