This is what I have ginned up. It plots the normals to the planar 3D curves
in a step file. I'm interested in improvements.


from OCC.Utils.DataExchange.STEP import STEPImporter

from OCC.Display.SimpleGui import *
from OCC import TopAbs
from OCC.TopExp import TopExp_Explorer
from OCC.TopoDS import TopoDS
from OCC.BRep import BRep_Tool
from OCC.BRepAdaptor import BRepAdaptor_Curve
from OCC import GeomAbs
from OCC.Geom import (Handle_Geom_BSplineCurve,
                      Handle_Geom_Direction,
                      Handle_Geom_Circle,
                      Geom_Circle)
from OCC.GeomLProp import GeomLProp_CLProps
from OCC.Precision import Precision
from OCC import gp
from OCC.TColgp import TColgp_Array1OfPnt
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.GeomAPI import GeomAPI_PointsToBSpline
from OCC.GeomConvert import (GeomConvert,
                             Convert)

import numpy as NP

def _Tcol_dim_1(li, _type):
    '''function factory for 1-dimensional TCol* types'''
    pts = _type(0, len(li)-1)
    for n,i in enumerate(li):
        pts.SetValue(n,i)
    return pts

def point2d_list_to_TColgp_Array1OfPnt(li):
    return _Tcol_dim_1(li, TColgp_Array1OfPnt)

def make_edge(shape):
    spline = BRepBuilderAPI_MakeEdge(shape)
    spline.Build()
    return spline.Shape()

def process_shape(shape):
    '''extracts edges from a shape'''
    edges = []
    edge_explorer = TopExp_Explorer(shape, TopAbs.TopAbs_EDGE)
    while edge_explorer.More():
        edge = TopoDS().Edge(edge_explorer.Current())
        cons = BRepAdaptor_Curve()
        cons.Initialize(edge)
        crv_type = cons.GetType()
        if crv_type == GeomAbs.GeomAbs_Circle:
            c = cons.Circle()
            h = Handle_Geom_Circle(Geom_Circle(c))
            h = GeomConvert().CurveToBSplineCurve(h)
            sign = 1.0
        elif crv_type == GeomAbs.GeomAbs_BSplineCurve:
            c = cons.BSpline()
            h = Handle_Geom_BSplineCurve(c)
            sign = -1.0
        else:
            pass
        crv = h.GetObject()
        props = GeomLProp_CLProps(crv.GetHandle(),
                                  1, # What does this Integer control?!
                                  Precision().Confusion())
        for u in NP.arange(cons.FirstParameter(),
                           cons.LastParameter()+0.001,
                           (cons.LastParameter() -
cons.FirstParameter())/100.0):
            props.SetParameter(u)
            d1 = gp.gp_Dir()
            props.Tangent(d1) # d is now the tangent vector
            z = gp.gp_Dir(0.0,0.0,1.0) # Curve is in a plane and
props.Normal does not seem to work
            d3 = d1.Crossed(z)
            print('%f,%f,%f,%f' %(u, d3.X(), d3.Y(), d3.Z()))
            origin = props.Value()
            array = []
            array.append(gp.gp_Pnt(origin.X(), origin.Y(), origin.Z()))
            array.append(gp.gp_Pnt(origin.X()+sign*400.0*d3.X(),
                                   origin.Y()+sign*400.0*d3.Y(),
                                   origin.Z()+sign*400.0*d3.Z()))
            xxx = point2d_list_to_TColgp_Array1OfPnt(array)
            SPL1 = GeomAPI_PointsToBSpline(xxx).Curve()
            edges.append(make_edge(SPL1))

        edge_explorer.Next()
    return edges

display, start_display, add_menu, add_function_to_menu = init_display()

my_step_importer = STEPImporter(r"simple.STEP")
my_step_importer.ReadFile()
print "Number of shapes:%i"%my_step_importer.GetNbrShapes()
the_shapes = my_step_importer.GetShapes()
display.DisplayShape(the_shapes)
normals = []
for shape in the_shapes:
    normals.extend(process_shape(shape))
display.DisplayColoredShape(normals, 'GREEN')
start_display()

On Thu, Jan 21, 2010 at 11:26 PM, Thomas Paviot <tpav...@gmail.com> wrote:

> 2010/1/21 Bryan Bishop <kanz...@gmail.com>
>
> On Thu, Jan 21, 2010 at 10:47 AM, Charles McCreary
>> <charles.r.mccre...@gmail.com> wrote:
>> > Now I want to extract some information from the imported geometry. The
>> > geometry consists of 3 b-spline curves with knots. I've perused the
>> doxygen
>> > generated documents but I'm still rather lost on how to begin. I'm
>> unclear
>> > on how to even start a simple task like extracting the points from the
>> > spline. Suggestions?
>>
>> Holy crap, a PE :-). Welcome aboard Charles. Let's see if this does the
>> trick:
>>
>> def process_shape(shape):
>>    '''extracts faces from a shape
>>    note: if this doesnt work you should try process_face(shape)
>>    returns a list of faces'''
>>    faces = []
>>    explorer = TopExp_Explorer(shape, TopAbs_FACE)
>>    while explorer.More():
>>        face = TopoDS().Face(explorer.Current())
>>        faces.append(face)
>>        explorer.Next()
>>    return faces
>>
>> def process_face(face):
>>    '''traverses a face for wires
>>    returns a list of wires'''
>>    wires = []
>>    explorer = TopExp_Explorer(face, TopAbs_EDGE)
>>    while explorer.More():
>>        edge = TopoDS().Edge(explorer.Current())
>>        wire = BRepBuilderAPI_MakeWire(edge).Wire()
>>        wires.append(wire)
>>        explorer.Next()
>>    return wires
>>
>> At least, this might get you a good head start: check out the
>> TopExp_Explorer class for sure. Another good technique is to use
>> bpython-interpreter.org, which can really help figure out pythonOCC's
>> deep internals without too much pain. Also, some of us pythonOCC users
>> are in #hplusroadmap on irc.freenode.net if you would like real-time
>> support over IRC.
>>
>> - Bryan
>> http://heybryan.org/
>> 1 512 203 0507
>>
>>
> Hi Bryan,
>
> You should definitely not use directly the TopExp_Explorer builtin OCC
> class but rather the Topology.py high level module. This implementation adds
> hash check of objects returned by TopExp_Explorer iterator to ensure that an
> element (either a vertex, wire etc.) matching the criterion (TopABS_VERTEX,
> TopAbs_WIRE etc.) is unique in the returned list.
>
> Regards,
>
> Thomas
>
>
> _______________________________________________
> Pythonocc-users mailing list
> Pythonocc-users@gna.org
> https://mail.gna.org/listinfo/pythonocc-users
>
>


-- 
Charles McCreary P.E.
CRM Engineering
903.643.3490 - office
903.224.5701 - mobile/GV
_______________________________________________
Pythonocc-users mailing list
Pythonocc-users@gna.org
https://mail.gna.org/listinfo/pythonocc-users

Reply via email to