tpaviot / pythonocc-core

Python package for 3D geometry CAD/BIM/CAM
GNU Lesser General Public License v3.0
1.39k stars 380 forks source link

Suggestions on a project with PythonOCC #1326

Closed Nadjei74 closed 5 months ago

Nadjei74 commented 6 months ago

Please I need suggestions on how I can do this with PythonOCC. I have a skinned surface that I want to attach a feature to. Because I want the feature to also have the curvature of the surface, I first want to create the profile of the feature(for example a box) on a plane, then project it to the surface to attain the curvature. After that I extrude the profile from the surface to attain my feature. I have attached a picture below of what I mean. Thanks in advance for the help. Screenshot 2024-05-15 135443

tpaviot commented 6 months ago

This issue is too general. Please insert any code snippet that can be tested if you need any help.

Nadjei74 commented 6 months ago

So I tried it using boolean section, where I first extrude a the profile of the feature through the skinned surface and with the section I am able to get the profiles on the Skinned surface. The problem now is how to access the profiles on the surface now.

This is how my code looks like (the part that does the sectioning and the part that tries to access these profiles.

testnew2.txt


from __future__ import print_function

import os
import os.path
import sys

from OCC.Core.STEPControl import STEPControl_Reader,STEPControl_Writer, STEPControl_AsIs
from OCC.Core.Interface import Interface_Static
from OCC.Core.TopTools import TopTools_ListOfShape
from OCC.Core.TopoDS import TopoDS_Edge
from OCC.Core.BRep import BRep_Tool
from OCC.Core.AIS import AIS_Shape
#from OCC.Core.TopTools import Top
from OCC.Core.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
from OCC.Core.GeomAbs import GeomAbs_Plane, GeomAbs_Cylinder
#from OCC.Core.TopoDS import topods_Face
from OCC.Core.BRepAdaptor import BRepAdaptor_Surface
from OCC.Display.SimpleGui import init_display
from OCC.Core.gp import (
gp_Pnt, gp_Vec,gp_Trsf,gp_Ax2,gp_Ax3,gp_Pnt2d,gp_Dir2d,gp_Ax2d,gp_Pln,gp_Circ,gp_Dir
)
from OCC.Core.BRepBuilderAPI import (
BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakeWire,BRepBuilderAPI_Transform
)
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism, BRepPrimAPI_MakeCylinder
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse, BRepAlgoAPI_Cut,BRepAlgoAPI_Section
from OCC.Extend.TopologyUtils import TopologyExplorer
from OCC.Core.STEPControl import STEPControl_Reader,STEPControl_Writer, STEPControl_AsIs
from OCC.Core.Interface import Interface_Static
from OCC.Core.GeomAPI import GeomAPI_IntSS
from math import pi

shp = read_step_file(
        os.path.join("..", "assets", "models", "face_recognition_sample_part.stp")
    )

def section():
    orign_profile = create_original(filename)
    sections = []
    for body in orign_profile:
        section_shp = BRepAlgoAPI_Section(shp, body, False)
        section_shp.ComputePCurveOn1(True)
        section_shp.Approximation(True)
        section_shp.Build()
        sections.append(section_shp)

    display.EraseAll()
    display.DisplayShape(shp)
    for section_ in sections:
        display.DisplayShape(section_.Shape())
    display.FitAll()
    return sections

def sect_edges():
    # Obtaining the intersecting edges from the sections
    sectioning = section()
    print(sectioning)
    #We loop through each sections created
    thewires=[]
    for i in range(len(sectioning)):
        intersect_edges = sectioning[i].SectionEdges()#obtaining the edges
        wire_builder = BRepBuilderAPI_MakeWire()
        # Iterate over the edges and add them to the wire builder
        for inter_edge in intersect_edges:
            topo_edge = TopoDS_Edge(inter_edge)
            wire_builder.Add(topo_edge)

        # Check if the wire is valid
        if wire_builder.IsDone():
            wire = wire_builder.Wire()
            thewires.append(wire)

            # Create an AIS_Shape for visualization
            anAisWire = AIS_Shape(wire)
            anAisWire.SetWidth(2.0)

            # Display the wire
            display.Context.Display(anAisWire, True)
        else:
            print("Error: Wire creation failed")
    return thewires

edgex=sect_edges()
print(edgex)

I tried to use the "sectioning.SectionEdges()" but I am unable to iterate through the edges created because the "ToptoolslistofShapeiterator" is not wrapped up in the PythonOCC 7.7.2 version. Screenshot 2024-05-20 132814 The profiles on the surface are what I am trying to make wire out of.

tpaviot commented 6 months ago

use TopTools_ListIteratorOfListOfShape

Nadjei74 commented 6 months ago

It is not wrapped up in the 7.7.2. Has it been changed in the 7.8.1?

tpaviot commented 6 months ago

Are you sure its not in 772? I confirm it is available in 781

Nadjei74 commented 6 months ago

tpavtool This is a picture I just took

Nadjei74 commented 6 months ago

For updating, I tried "conda create --name=pyoccenv python=3.10 source activate pyoccenv conda install -c conda-forge pythonocc-core=7.8.1"

from the Readme page but it doesn't seem to work

tpaviot commented 6 months ago

what do you mean with "doesn' seem to work?". Be careful, you need TopTools_ListIteratorOfListOfShape, check the spelling

Nadjei74 commented 6 months ago

No I'm talking about installing pythonOCC 7.8.1 using the conda package.

tnakaicode commented 5 months ago

BRepAlgoAPI_Section.SectionEdges() returns TopoTools_ListOfShape, and we need another conversion method for TopTools_ListIteratorOfListOfShape.

We need the method to convert TopoTools_ListOfShape to TopTools_ListIteratorOfListOfShape, or TopoTools_ListOfShape must be made Iterable.

display, start_display, add_menu, add_function_to_menu = init_display()

shp = read_step_file("../assets/models/face_recognition_sample_part.stp"
                     # os.path.join("..", "assets", "models", "face_recognition_sample_part.stp")
                     )

def section():
    #orign_profile = create_original(filename)
    sections = []
    for body in range(3):
        section_shp = BRepAlgoAPI_Section(shp, gp_Pln(gp_Pnt(0,0,body+1), gp_Dir(0,0,1)), False)
        section_shp.ComputePCurveOn1(True)
        section_shp.Approximation(True)
        section_shp.Build()
        section_edge = section_shp.SectionEdges()
        print(section_edge.Size())
        for i in section_edge:
            print(i)
        sections.append(section_shp)

image

tpaviot commented 5 months ago

@tnakaicode to loop over a TopoDS_ListOfShape you need to instanciate a TopoDS_ListIteratorOfListOfShape, for example:

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.TopTools import TopTools_ListOfShape, TopTools_ListIteratorOfListOfShape

shp1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape()
shp2 = BRepPrimAPI_MakeBox(20, 30, 40).Shape()

los = TopTools_ListOfShape()
#print(dir(los))
los.Append(shp1)
los.Append(shp2)

it = TopTools_ListIteratorOfListOfShape(los)

while it.More():
    next_shp = it.Value()
    print(next_shp)
    it.Next()

output:

$ python tst_issue_1326.py 
<class 'TopoDS_Solid'>
<class 'TopoDS_Solid'>
Nadjei74 commented 5 months ago

Just installed the pythonOCC core781 from conda forge and the "TopTools_ListIteratorOfListOfShape" is still not wrapped. pav1

tnakaicode commented 5 months ago

TopTools_ListIteratorOfListOfShape --> not defined in TopTools.pyi, but defined in TopTools.py TopTools_ListIteratorOfListOfListOfShape --> declared not to be defined in TopTools.pyi

we need TopTools_ListIteratorOfListOfShape.

Nadjei74 commented 5 months ago

Sorry but I don't understand what you mean @tnakaicode. I copied the code @tpaviot wrote and pasted it in a python file. It did not work or there is something else I have to do again?

tpaviot commented 5 months ago

@Nadjei74 don't be confused between TopTools_ListIteratorOfListOfShape and TopTools_ListIteratorOfListOfListOfShape, it is not the same thing.

@tnakaicode comment is pretty clear. The code snippet I posted works on 7.8.1. 7.8.1 can be installed easily using conda-forge. It's up to you right now @Nadjei74 . I thnk the issue can be closed, because the answer got its reply.

Nadjei74 commented 5 months ago

Thanks for the help