-
I am a python user and I don't know C++. I always get this error when I develop with this library. Can anyone help me? How can I avoid this error? For example, the following code def add_Div_line(start_point: Ge.Point3d, end_point: Ge.Point3d, layer_name: str=None, model: Db.BlockTableRecord =None):
# if not db:
# db = Db.curDb()
if not model:
model = Db.BlockTableRecord(Db.curDb().modelSpaceId(), Db.OpenMode.kForWrite)
if not layer_name:
layer_name = "DivSpanLine_locked"
if not isinstance(start_point, Ge.Point3d):
raise TypeError("start_point should be PyGe.Point3d")
if not isinstance(end_point, Ge.Point3d):
raise TypeError("end_point should be PyGe.Point3d")
line = Db.Line(start_point, end_point)
line.setDatabaseDefaults()
line.setLayer(layer_name)
# db.addToModelspace(line)
lineid = model.appendAcDbEntities(line)
line.close()
return lineid At all, thank you very much for your help |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
eLockViolation means that AutoCAD is asking you to lock the drawing to protect it from other routines from modifying it at the same time. You will need to always lock the document when working from a modeless dialog, or in a session context. There are a couple of ways you can do this, here are two examples # use this if want to lock a document other than the current
def fun_with_lock1(startPoint, endPoint):
try:
docman = Ap.DocManager()
doc = docman.mdiActiveDocument()
docman.lockDocument(doc)
# addline
finally:
docman.unlockDocument(doc)
# use this if want to lock the current document
def fun_with_lock2(startPoint, endPoint):
#unlock at garbage collection
lock = Ap.AutoDocLock()
#add line |
Beta Was this translation helpful? Give feedback.
-
Opening modelspace can become a pain , I added Database.addToModelspace to make life easy, so these two functions are the same def addline1(db : Db.Database):
lock = Ap.AutoDocLock()
line = Db.Line(Ge.Point3d(0,0,0), Ge.Point3d(0,100,0))
model = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.kForWrite )
return model.appendAcDbEntity(line)
def addline2(db : Db.Database):
lock = Ap.AutoDocLock()
line = Db.Line(Ge.Point3d(10,0,0), Ge.Point3d(10,100,0))
return db.addToModelspace(line) |
Beta Was this translation helpful? Give feedback.
-
It’s really good idea to limit the time a database object is opened (kForRead, or kForWrite), what will eventually happen is you will have modelspace opened for read, then you attempt to open it for write, then you will close it while some other function still thinks its open. Best practice is to get in and get out. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your answer. |
Beta Was this translation helpful? Give feedback.
Opening modelspace can become a pain , I added Database.addToModelspace to make life easy, so these two functions are the same