-
Can it be done better? def get_objs_on_layer(db: Db.Database, layers: tuple[str, ...]):
mbtr = Db.BlockTableRecord(db.modelSpaceId())
for id in mbtr.objectIds():
ent = Db.Entity(id)
if ent.layer() in layers:
yield id |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
That’s pretty much it. If it’s the current drawing, you can use a selection set filter. But you will find that iterating is actually faster than selection sets, plus the code is reusable with side databases. Even though opening entities for read is relatively cheap, if your searching for multiple properties, maybe move the loop to the outside, something like def has_proprty_layer(ent : Db.Entity, filter : dict) -> bool:
return ent.layer() in filter["layer"]
def has_proprty_color_index(ent : Db.Entity, filter : dict) -> bool:
return ent.colorIndex() in filter["color"]
@command
def doit():
db = Db.curDb()
filter = {}
filter["layer"] = set(["0", "2"])
filter["color"] = set([1, 2, 3])
model = Db.BlockTableRecord(db.modelSpaceId())
for ent in [Db.Entity(id) for id in model.objectIds()]:
if has_proprty_layer(ent,filter):
print("has layer = true")
if has_proprty_color_index(ent,filter):
print("has color = true") |
Beta Was this translation helpful? Give feedback.
-
this is one really cool feature in the C# language, LINQ , where you can run expressions on a list I.e. |
Beta Was this translation helpful? Give feedback.
That’s pretty much it. If it’s the current drawing, you can use a selection set filter. But you will find that iterating is actually faster than selection sets, plus the code is reusable with side databases.
Even though opening entities for read is relatively cheap, if your searching for multiple properties, maybe move the loop to the outside, something like