andreasplesch / OCCToX3D

convert OCC (step, iges, stl ..) to X3D
https://mybinder.org/v2/gh/andreasplesch/OCCtoX3D/conda2
GNU Lesser General Public License v2.1
4 stars 1 forks source link

try VrmlAPI.writeDoc #4

Open andreasplesch opened 4 years ago

andreasplesch commented 4 years ago

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).

tpaviot commented 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.)
andreasplesch commented 4 years ago

thanks. Hm, does not load anything. Perhaps there is something which needs to be done with the document.

tpaviot commented 4 years ago

Is the vrml file generated ?

andreasplesch commented 4 years ago

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>
andreasplesch commented 4 years ago

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blob;f=src/VrmlData/VrmlData_ShapeConvert.cxx;h=bf5d86f086674aa2aef84117a7d04d79929565d4;hb=refs/heads/master#l871

is how VrmlAPI wants to use the Doc

andreasplesch commented 4 years ago

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.

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blob;f=src/VrmlData/VrmlData_ShapeConvert.cxx;h=bf5d86f086674aa2aef84117a7d04d79929565d4;hb=refs/heads/master#l619

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.

tpaviot commented 4 years ago

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.

tpaviot commented 4 years ago

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.

tpaviot commented 4 years ago

screenshots/doc at https://dblanding.github.io/kodacad/

andreasplesch commented 4 years ago

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:

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blob;f=src/VrmlData/VrmlData_ShapeConvert.cxx;h=bf5d86f086674aa2aef84117a7d04d79929565d4;hb=refs/heads/master#l592

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.

andreasplesch commented 4 years ago

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blobdiff;f=src/VrmlData/VrmlData_ShapeConvert.cxx;h=08b96b8cb45583ea499b07b36151141ee95dcf11;hp=151d1978b18ac225979b53ca745d8cd52c11c593;hb=a3506de7706346d9b0e472f30aac5c55bbb635d0;hpb=eaac086605f1b03a064cfa0b0b4e7fe7d07d1356

is the commit which introduced writeDoc

andreasplesch commented 4 years ago

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blob;f=samples/mfc/standard/Common/ImportExport/ImportExport.cpp;h=c55dd5b0761d42372280be10841dbc4d67f77518;hb=a3506de7706346d9b0e472f30aac5c55bbb635d0#l421

is how the occ example reads step.

there is a transfer of multiple roots.

But this does not use a document.

andreasplesch commented 4 years ago

http://git.dev.opencascade.org/gitweb/?p=occt.git;a=blob;f=src/XDEDRAW/XDEDRAW_Common.cxx;h=986be6bd88f5acf559ab288b16c4ff27cd3e117e;hb=a3506de7706346d9b0e472f30aac5c55bbb635d0#l308

is how XDEDRAW reads step, into a document.

tpaviot commented 4 years ago

That's basically the python code I posted above

andreasplesch commented 4 years ago

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 ?

andreasplesch commented 4 years ago

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

andreasplesch commented 4 years ago

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.

andreasplesch commented 4 years ago

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
andreasplesch commented 4 years ago

I asked on the forum:

https://www.opencascade.com/content/writevrml-works-only-after-xdisplay

tpaviot commented 4 years ago

can you please post here the reply (if ever you have one) ?

andreasplesch commented 4 years ago

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.

andreasplesch commented 4 years ago

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
andreasplesch commented 4 years ago

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 ?

tpaviot commented 4 years ago

Yes, the BRepMesh_IncrementalMesh class, see https://github.com/tpaviot/pythonocc-core/blob/master/src/Tesselator/ShapeTesselator.cpp#L99

andreasplesch commented 4 years ago

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.