Replies: 25 comments 66 replies
|
Sure thing, Here's a quick sample. import traceback
from pyrx_imp import Rx, Ge, Db, Ap, Ed, Gi, Gs
# create a new record and at it to the database blocktable
def makeBlockRecord(db: Db.Database, name):
id = Db.ObjectId()
bt = Db.BlockTable(db.blockTableId(), Db.OpenMode.kForWrite)
if name in bt:
return
newBlock = Db.BlockTableRecord()
newBlock.setName(name)
id = bt.add(newBlock)
return id
# add some geometry
def createXBLOCK(db: Db.Database):
blkid = makeBlockRecord(db, "XBLOCK")
blk = Db.BlockTableRecord(blkid, Db.OpenMode.kForWrite)
line1 = Db.Line(Ge.Point3d(0.0, 0.0, 0.0), Ge.Point3d(100, 100, 0))
line2 = Db.Line(Ge.Point3d(100.0, 0.0, 0.0), Ge.Point3d(0.0, 100.0, 0.0))
blk.appendAcDbEntity(line1)
blk.appendAcDbEntity(line2)
return blkid
def PyRxCmd_doit():
try:
db = Db.curDb()
blkid = createXBLOCK(db)
# create an 'insert' or reference
ref = Db.BlockReference(Ge.Point3d(0.0, 0.0, 0.0), blkid)
db.addToModelspace(ref)
except Exception as err:
traceback.print_exception(err) |
Grouping objects@CEXT-Dan : there is a sample where you created a group with objects attached. How would one delete that group including all objects attached? I would start db = Db.curDb()
groups = Db.Dictionary(db.groupDictionaryId(), Db.OpenMode.kForWrite)
if groups.has(handle): # if handle exists delete group |
|
How to get the id of all objects contained on a sheet (not on the modelspace), e.g. |
|
You got it, if you need to rename or delete a layout on a remote database import traceback
from pyrx_imp import Ap, Db, Ed, Ge, Gi, Gs, Rx
def PyRxCmd_doit():
try:
db = Db.curDb()
deleteLayouts(db,["Layout2", "Layout3"])
except Exception:
traceback.print_exc()
def deleteLayouts(db : Db.Database, layoutList):
try:
man = Db.LayoutManager()
token = man.setupForLayouts(db)
for layout in layoutList:
if man.layoutExists(layout,db):
man.deleteLayout(layout,db)
finally:
man.clearSetupForLayouts(token) |
Check for coordinate systemHello. In BCAD I am trying to check if a geo coordinate system is already set, ifso fetch and step further. I approach as such: if not db.geoCoordinateSystemId():
print("\nWe have none")
print(db.geoCoordinateSystemId())Following error is thrown: RuntimeError:
Exception, Not implemented in BRX!, function geoCoordinateSystemId ,Line 974, File PyDbDatabase.cpp:Any hints on how to reach there differently? |
Behavior control of built-in commandsHow can I control the behavior of built-in commands, such as changing the layer on which a specific type of element is added, e.g. dimensions are added on the |
Get EPSG-codeWhen fetching EPSG code (geo information) I do. def getGeoDataId(db: Db.Database) -> Db.ObjectId:
if not Db.Core.hasGeoData(db):
data = Db.GeoData()
data.setBlockTableRecordId(db.modelSpaceId())
return data.postToDb()
return Db.Core.getGeoDataObjId(db)
def PyRxCmd_doit():
try:
db = Db.curDb()
geoDataId = getGeoDataId(db)
if geoDataId.isNull():
return
geoData = Db.GeoData(geoDataId, Db.OpenMode.kForRead)
geoCS = xmltodict.parse(geoData.coordinateSystem())
epsg = geoCS['Dictionary']['Alias'][0]['@id']Surely there is more elegant around, which I am not aware of. |
Adding LayerTableRecords to LayerTableWhen trying lt = Db.LayerTable(db.layerTableId(), Db.OpenMode.kForRead)
if not lt.hasFields:
return
for index, row in df.iterrows():
if not lt.has(row["Name"]):
lt.upgradeOpen()
print("\nAdding a new layer")
ltr = Db.LayerTableRecord()
ltr.setName(row["Name"])
lt.add(ltr)
ltr.close()
lt.downgradeOpen()I get lt.upgradeOpen()
RuntimeError:
Exception!(eInvalidOpenState) in function upgradeOpen, Line 204, File PyDbObject.cpp: Hints appreciated. |
Much appreciated. Works like a charmer. Now I am trying to alter the function to update existing layer colors like this: def updateLayers(db, layers):
layerMap = {}
clr = Db.Color()
lt = Db.LayerTable(db.layerTableId(), Db.OpenMode.kForWrite)
for layer in layers:
if lt.has(layer[0]):
ltrid = lt.getAt(layer[0])
ltr = Db.LayerTableRecord(ltrid)
clr.setColorIndex(layer[1])
ltr.setColor(clr)
else:
ltr = Db.LayerTableRecord()
ltr.setName(layer[0])
clr.setColorIndex(layer[1])
ltr.setColor(clr)
layerMap[layer[0]] = lt.add(ltr)
return layerMapLayer table record is not updated. Cannot find any |
Built-in commandsCan I use a built-in command like |
Add curve3d to modelspaceI have derived an object type curve3d in BCAD from an halignment. I suppose this being an entity of all sorts.
Am trying to push that to model space cv = halign.getAcGeCurve()
db = Db.curDb()
db.addToModelspace(cv)An error is thrown db.addToModelspace(cv)
Boost.Python.ArgumentError: Python argument types in
Database.addToModelspace(Database, Curve3d)
did not match C++ signature:
addToModelspace(class PyDbDatabase {lvalue}, class boost::python::list)
addToModelspace(class PyDbDatabase {lvalue}, class PyDbEntity {lvalue})Hints on how to correctly push the entity to model space is appreciated |
Geometric vertex dependenciesHow would one create/destroy dependencies betwee two entities through a common vertex (for example a polyline or an alignment? have attached a video for further understanding using BCADs parametric mechanisms. One vertex is fixed as a helper in order for the line not to move entirely. Ideas welcome. PL-dependencies.mp4 |
Getting geometry from sectionDear all, I am trying to get sections and their according geometric parameters in order to generate input to views. I have started out like this: managerId = db.getSectionManagerId()
manager = Db.SectionManager(managerId)
mIds = manager.objectIds()
for id in mIds:
sec = Db.Section(id)Now I am stuck. Hints to continue appreciated. Best Seb |
|
maybe use the section normal for the ViewDirection import traceback
from pyrx_impx import Rx, Ge, Gi, Db, Ap, Ed, Ax
def write_vtr_for_section(sec, vtable):
vt = vtable
if vt.has(sec.getName()):
vtrid = vt.getAt(sec.getName())
vtr = Db.ViewTableRecord(vtrid, Db.OpenMode.kForWrite)
print("entry exists")
else:
vtr = Db.ViewTableRecord()
vtr.setName(sec.getName())
vt.upgradeOpen()
vt.add(vtr)
print("creating new entry")
ex = Db.Extents()
sec.bounds(ex)
vtr.setTarget(ex.midPoint())
vtr.setViewDirection(sec.normal())
Ed.Core.vportTableRecords2Vports() # needed?
def PyRxCmd_doit():
try:
db = Db.curDb()
vtableId = db.viewTableId()
vtable = Db.ViewTable(vtableId)
managerId = db.getSectionManagerId()
manager = Db.SectionManager(managerId)
mIds = manager.objectIds()
for id in mIds:
sec = Db.Section(id)
write_vtr_for_section(sec, vtable)
except Exception as err:
traceback.print_exception(err) |
Set Color to ByBlockDear all, I am trying to set the color to ByColor for the parent blocks of block references programmatically. Hints appreciated. import traceback
from pyrx_imp import Ap, Db, Ed, Ge, Gi, Gs, Rx
print("added command = blockref2byblock")
@Ap.Command()
def blockref2byblock():
try:
db = Db.curDb()
ids = db.objectIds(Db.BlockReference.desc())
for id in ids:
print(id)
print(Db.BlockReference(id).getBlockName())
# Lost in space
except Exception as err:
traceback.print_exception(err) |
Trim sideloaded DWG by closed polylineLooking to script the following behaviour:
Any assistance for 3. is greatly appreciated. The blocks to be trimmed may be over 100M in size. |
|
3, is pretty difficult. Trim uses AcDbCurve.getSplitCurves, so only objects that derive from curve can be split. Blocks add another layer of complexity as you can’t trim a block, and they can have their own coordinate systems if the reference is scaled or rotated. If the block has only one reference, and the coordinate systems are aligned. Then you can split the curves inside the block. If the block is scaled or rotated, then you will have to transform either the curve, or the boundary to match. Otherwise, you’ll probably have to explode the reference, split the curves, then create new blocks and references to replace them 100G? if by this you mean extraordinarily large files, if BricsCAD can open the files without issue, then you have a chance. If there’s a lot of memory pressure, then breaking the code up is probably a good idea, so items can be garbage collected |
|
I noticed that @CEXT-Dan's samples concerning multithreading do not use the list data type. I have a suspicion that the list data type is impossible to use in another thread, no matter the case. However, I'd like a second opinion from someone more experienced. |
Create anonymous blockI am trying to create an anonymous block doing def makeAnonBlock(db: Db.Database):
bt = Db.BlockTable(db.blockTableId())
newBlock = Db.BlockTableRecord()
print(newBlock.isAnonymous())
newBlock.setName("*TEST")
print(newBlock.isAnonymous())
bt.upgradeOpen()It returns Hints appreciated. Best Seb |
|
AutoCAD wants Anonymous Block created with *U It would not let me deep clone blocks, I noticed the recommendation is to create a new empty block, then deep clone the contents. import traceback
from pyrx import Ap, Db, Ge, Ed
def makeAnonymousBlock(db: Db.Database):
btr = Db.BlockTableRecord()
btr.setName("*U")
bt = Db.BlockTable(db.blockTableId(), Db.OpenMode.kForWrite)
btrid = bt.add(btr)
circle = Db.Circle(Ge.Point3d(0, 0, 0), Ge.Vector3d.kZAxis, 5)
btr.appendAcDbEntity(circle)
return btrid
# make an empty block
def makeEmptyAnonymousBlock(db: Db.Database):
btr = Db.BlockTableRecord()
btr.setName("*U")
bt = Db.BlockTable(db.blockTableId(), Db.OpenMode.kForWrite)
return bt.add(btr)
def cloneAnonymousBlockContents(db: Db.Database, originalbtrid, emptybtrid):
mapping = Db.IdMapping()
mapping.setDestDb(db)
obtr = Db.BlockTableRecord(originalbtrid)
db.deepCloneObjects(obtr.objectIds(), emptybtrid, mapping)
@Ap.Command("doit")
def clone_wars():
try:
db = Db.curDb()
originalbtrid = makeAnonymousBlock(db)
emptybtrid = makeEmptyAnonymousBlock(db)
cloneAnonymousBlockContents(db,originalbtrid,emptybtrid)
bt = Db.BlockTable(db.blockTableId())
print(bt.asDict())
except Exception as err:
traceback.print_exception(err) |
|
I'd like to create a command that lists all xrefs in a DB in the form of |
|
Is there a native event listener for xref? I'd like to listen for a status change. It seems that AutoCAD does listen for an xref change, as it gives me a notification whenever I save my changes to a file that I reference in another tab. After searching through the ObjectARX reference, the closest thing I could find was a reactor that could listen for xref subcommands. I might be able to use RxVariableReactor though |
|
Is there any way to run a http server in the Runtime Extension? I tried using threads, but to no avail. |
Union to solidsDear all, I am tryint to union solids from block references as follows: for ref_id in ms.objectIds(Db.BlockReference.desc()):
blockref = Db.BlockReference(ref_id)
logger.info("Block reference: %s", blockref)
parts = blockref.explode()
solids = []
for part in parts:
if part.isA() == Db.Solid3d.desc():
solids.append(part)Unfortunately I cannot find a sample to unite the list of solids afterwards. Any hints appreciated. |








Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Create block with primitives
Thanks for everything. Hints on how to create a block and add primitives are appreciated. Best Sebastian
All reactions