I have a python program that I am developing using RevitPythonShell and one task that it does is to establish a COM link to an Autocad file using the ObjectDBX.AxDBDocument interface. The program will then read the database for various graphical entities and may, depending on the file's content, create or delete entities in that file.
To handle the COM access to Autocad, I have created a Python class with __exit__ and __enter__ functions that when used with Python's with structure will automatically upon exit, close the DWG file and takedown the COM link.
This works as expected except when I added one particular function to my program - when that function is active, the program will proceed without error but DWG is locked even though the program has finished execution. That is, the program is done but trying to open the DWG file with Autocad gives a "Open File as read-only?" message. To release the lock, I have to completely shut down Revit. Shutting down RevitPythonShell is not enough.
Here is the offending function:
def CreateAttributeReplacements (BlkRef, MSpace):
#MSpace = BlkRef.Document.ModelSpace
Attr = BlkRef.GetAttributes()
CAttr = BlkRef.GetConstantAttributes()
Layer = BlkRef.Layer
LineType = BlkRef.Linetype
LTScale = BlkRef.LinetypeScale
Color = BlkRef.TrueColor
Rst = []
#return Rst ----> if I short-circuit function here, things work as expected.
print "CreateAttr"
for item in Attr:
if item.MTextAttribute:
temp = MSpace.AddMText (item.InsertionPoint, item.MTextBoundaryWidth, item.MTextAttributeContent)
temp.DrawingDirection = item.MTextDrawingDirection
temp.Height = item.Height
else:
temp = MSpace.AddText (item.TextString, item.InsertionPoint, item.Height)
temp.Alignment = item.Alignment
temp.Backward = item.Backward
temp.ScaleFactor = item.ScaleFactor
#temp.TextGenerationFlag = item.TextGenerationFlag #should work but does not.
temp.UpsideDown = item.UpsideDown
temp.ObliqueAngle = item.ObliqueAngle #Should work but does not.
#Common properties
temp.Layer = Layer
temp.Linetype = LineType
temp.LinetypeScale = LTScale
temp.TrueColor = Color
temp.StyleName = item.StyleName
Rst.append (temp)
temp = None
return Rst
The problem seems to have something to do with the Add.Text and Add.MText part of the function. If I short-circuit the function with a return statement at the place shown, the problem goes away.
Alternately, if I do not short-circuit the function but instead open a second file - which automatically closes the first file - the problem goes away.
So, does anyone know what I might be doing wrong? Or has anyone seen this problem before? Is it a bug?
Sincerely; Michelle
PS: I am using Revit 2019 and Autocad 2022 (office) or Revit 2019 and Autocad 2016 (home)