2

Let's suppose I'm using this STEP file data as input:

#417=ADVANCED_FACE('face_1',(#112),#405,.F.);
#418=ADVANCED_FACE('face_2',(#113),#406,.F.);
#419=ADVANCED_FACE('face_3',(#114),#407,.F.);

I'm using pythonocc-core to read the STEP file. Then the following code will print the names of the ADVANCED_FACE instances (face_1,face_2 and face_3):

from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE
from OCC.Core.StepRepr import StepRepr_RepresentationItem

reader = STEPControl_Reader()
tr = reader.WS().TransferReader()
reader.ReadFile('model.stp')
reader.TransferRoots()
shape = reader.OneShape()


exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
    s = exp.Current()
    exp.Next()

    item = tr.EntityFromShapeResult(s, 1)
    item = StepRepr_RepresentationItem.DownCast(item)
    name = item.Name().ToCString()
    print(name)

How can I access the identifiers of the individual shapes? (#417,#418 and #419)

Minimal reproduction

https://github.com/flolu/step-occ-instance-ids

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137

2 Answers2

1

Create a STEP model after reader.TransferRoots() like this:

model = reader.StepModel()

And access the ID like this in the loop:

id = model.IdentLabel(item)

The full code looks like this and can also be found on GitHub:

from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE
from OCC.Core.StepRepr import StepRepr_RepresentationItem

reader = STEPControl_Reader()
tr = reader.WS().TransferReader()
reader.ReadFile('model.stp')
reader.TransferRoots()
model = reader.StepModel()
shape = reader.OneShape()


exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
    s = exp.Current()
    exp.Next()

    item = tr.EntityFromShapeResult(s, 1)
    item = StepRepr_RepresentationItem.DownCast(item)

    label = item.Name().ToCString()
    id = model.IdentLabel(item)

    print('label', label)
    print('id', id)

Thanks to temurka1 for pointing this out!

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
0

I was unable to run your code due to issues installing the pythonocc module, however, I suspect that you should be able to inspect the StepRep_RepresentationItem object (prior to string conversion) by traversing __dict__ on it to discover/access whatever attributes/properties/methods of the object you may need:

    entity = tr.EntityFromShapeResult(s, 1)
    item = StepRepr_RepresentationItem.DownCast(entity)
    
    print(entity.__dict__)
    print(item.__dict__)

If necessary the inspect module exists to pry deeper into the object.

References

https://docs.python.org/3/library/stdtypes.html#object.__dict__

https://docs.python.org/3/library/inspect.html

https://github.com/tpaviot/pythonocc-core/blob/66d6e1ef6b7552a1110a90e86a1ed34eb12ecf16/src/SWIG_files/wrapper/StepElement.pyi

pygeek
  • 7,356
  • 1
  • 20
  • 41
  • Unfortunately it seems as if `entity` and `item` are just pointers. The prints log the following: `{'this': }` and `{'this': }` – Florian Ludewig Jul 26 '22 at 09:11
  • @Flo I see, this makes sense, considering the python interface files. What do you get with `dir(entity)` and `dir(item)`? I'm willing to chat offline concerning this—this question seems entirely solvable, but I'm not able to get it running on my machine. – pygeek Jul 26 '22 at 16:20
  • @Flo, also, I found that you may be able to dump the object to a string `.DumpToString()` or json `.DumpJsonToString()`. https://github.com/tpaviot/pythonocc-core/blob/e8af62e79b9c0449ea04f08b6d70b8edf5f54675/test/core_wrapper_features_unittest.py#L728 it's also worth inspecting the corresponding cpp interface files. – pygeek Jul 26 '22 at 16:37
  • `dir()` doesn't contain information about the id. I would be glad to chat privately! Then we could also fix your problem with not being able to run the code. And `.DumpToString()` does only work for the shape, but doesn't contain information about the id, too. – Florian Ludewig Jul 26 '22 at 18:49
  • @Flo Are you able to invite me to a chat. I'm having trouble inviting you. Or we can just chat in the Python section—just @ me. – pygeek Jul 28 '22 at 00:06
  • I'm not sure where the chat is – Florian Ludewig Jul 29 '22 at 06:52
  • Where would you like to chat? – Florian Ludewig Aug 04 '22 at 10:33
  • @Flo I messaged you in the Python channel of SO chat. Just respond with the time in GMT you want to sync. – pygeek Aug 06 '22 at 05:23