tpaviot / pythonocc-core

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

Projection of a point on a curve #1137

Open fractal97 opened 2 years ago

fractal97 commented 2 years ago

I used to have this working on OCE 0.18 for Python 2.6 . But now, OCE 0.18.1 compiled for Python 2.7 or OCC 7.6.2 for Python 3.6.8, it is failing or not giving any results.

import sys
import OCC
occ_ver = OCC.VERSION
print("occ_ver =",occ_ver)
py_ver = sys.version_info
print("py_ver = ", py_ver)

if occ_ver == '7.6.2':
    from OCC.Core.gp import  gp_Pnt,gp_Vec
    from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
else:
    from OCC.gp import  gp_Pnt,gp_Vec
    from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge

from OCCUtils import Common

crv = [(0,0,0),(.5,0,0),(2,0,0),(2.5,0,0)]
array=[]
for p in crv:
    array.append(gp_Pnt(p[0],p[1],p[2]))  
start_tangent=gp_Vec(array[0],array[1])
end_tangent=gp_Vec(array[-2],array[-1])
c=Common.interpolate_points_to_spline(array,start_tangent,end_tangent)
p=gp_Pnt(1,.1,0)
ced=BRepBuilderAPI_MakeEdge(c).Edge()
rrr=Common.project_point_on_curve(ced,p)
print(rrr)
print("Done")
#----- OUTPUT----
('occ_ver =', '0.18.1')
('py_ver = ', sys.version_info(major=2, minor=7, micro=18, releaselevel='final', serial=0))
Traceback (most recent call last):
  File "fails.py", line 26, in <module>
    rrr=Common.project_point_on_curve(ced,p)
  File "c:\Python27\lib\site-packages\OCCUtils\Common.py", line 558, in project_point_on_curve
    rrr = GeomAPI_ProjectPointOnCurve(pnt, crv)
  File "c:\Python27\lib\site-packages\OCC\GeomAPI.py", line 659, in __init__
    _GeomAPI.GeomAPI_ProjectPointOnCurve_swiginit(self, _GeomAPI.new_GeomAPI_ProjectPointOnCurve(*args))
RuntimeError: Standard_NullObject

#---- OUTPUT----
occ_ver = 7.6.2
py_ver =  sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
#---------------------------------------------------------------------------------------------

Also, BRepAdaptor_HCurve and BRepAdaptor_HCompCurve don't exist any more in 7.6.2, so there is an error in Common with import. Furthermore, pts.thisown = False doesn't work anymore in interpolate_points_to_spline() in Common.py

thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag") AttributeError: 'TColgp_HArray1OfPnt' object has no attribute 'own'

tpaviot commented 2 years ago

The OCCUtils package is deprecated and is not maintained anymore. If you need to project a point on a curve, have a look at the example https://github.com/tpaviot/pythonocc-demos/blob/master/examples/core_geometry_project_point_on_curve.py