Message 1 of 7
Not applicable
09-16-2016
05:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi All,
I have created function which creates block using external drawing file, I have used following code. It creates the block properly but after creating block in the current active document auto cad hangs.
Public Sub InsertDrawingAsBlock(doc As Document, path As String, blockname As String, ipt As Point3d, ByVal scalefator As Scale3d, ByVal rotation As Double, ByVal xmlAttribList As XmlNodeList)
'current drawing database
Dim currentdb As Database = doc.Database
'current drawing's editor
Dim editor As Editor = doc.Editor
Dim lock As DocumentLock = doc.LockDocument()
Dim strAttribTag As String
Dim strAttribValue As String
Using lock
Dim blockid As ObjectId = ObjectId.Null
' create database to read dwg file
Dim sourcedb As New Database(False, True)
Using sourcedb
sourcedb.ReadDwgFile(path, System.IO.FileShare.Read, True, "")
blockid = currentdb.Insert(path, sourcedb, True)
Using tr As Transaction = doc.TransactionManager.StartTransaction()
Dim bt As BlockTable = DirectCast(tr.GetObject(currentdb.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(blockname) Then
' MessageBox.Show(string.Format("Block {0} does already exist\nTry another block or Exit", blockname));
Return
End If
bt.UpgradeOpen()
Dim btrec As BlockTableRecord = DirectCast(blockid.GetObject(OpenMode.ForRead), BlockTableRecord)
btrec.UpgradeOpen()
btrec.Name = blockname
btrec.DowngradeOpen()
'---> debug only
' this code block is written in the good programming manner (remember that)
'For Each index As ObjectId In btrec
' Dim en As Entity = DirectCast(index.GetObject(OpenMode.ForRead), Entity)
' Dim adef As AttributeDefinition = TryCast(en, AttributeDefinition)
' If adef IsNot Nothing Then
' editor.WriteMessage(vbLf + adef.Tag)
' End If
'Next
'<--- debug only
Dim btr As BlockTableRecord = DirectCast(currentdb.CurrentSpaceId.GetObject(OpenMode.ForWrite), BlockTableRecord)
Using btr
Using bref As New BlockReference(ipt, blockid)
Dim mat As Matrix3d = Matrix3d.Identity
bref.TransformBy(mat)
bref.ScaleFactors = scalefator
bref.Rotation = rotation
btr.AppendEntity(bref)
tr.AddNewlyCreatedDBObject(bref, True)
Using btAttRec As BlockTableRecord = DirectCast(bref.BlockTableRecord.GetObject(OpenMode.ForRead), BlockTableRecord)
Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection
For Each subid As ObjectId In btAttRec
Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)
Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)
If attDef IsNot Nothing Then
' ed.WriteMessage("\nValue: " + attDef.TextString);
Dim attRef As New AttributeReference()
attRef.SetPropertiesFrom(attDef)
attRef.Visible = attDef.Visible
attRef.SetAttributeFromBlock(attDef, bref.BlockTransform)
attRef.HorizontalMode = attDef.HorizontalMode
attRef.VerticalMode = attDef.VerticalMode
attRef.Rotation = attDef.Rotation
attRef.TextStyleId = attDef.TextStyleId
attRef.Position = attDef.Position + ipt.GetAsVector()
attRef.Tag = attDef.Tag
attRef.FieldLength = attDef.FieldLength
If xmlAttribList IsNot Nothing Then
For Each xmlAttrib In xmlAttribList
strAttribTag = GetXMLAttribute(xmlAttrib, "tag")
strAttribValue = GetXMLAttribute(xmlAttrib, "value")
If attDef.Tag = strAttribTag Then
' set the attribute text to to the correct string
attRef.TextString = strAttribValue
End If
Next xmlAttrib
End If
atcoll.AppendAttribute(attRef)
tr.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End Using
bref.DowngradeOpen()
End Using
End Using
btrec.DowngradeOpen()
bt.DowngradeOpen()
'editor.Regen()
tr.TransactionManager.QueueForGraphicsFlush()
doc.TransactionManager.FlushGraphics()
tr.Commit()
End Using
End Using
End Using
End SubI have also executed the profiler and found that acad.exe takes more time. (Please find attached screenshot of the profiler) As per the profiler some "Malloc" funciton from ACGE20.dll (auto cad file) takes more CPU time for execution.
Solved! Go to Solution.