Originally Posted by
matthew.proud
Ok - stuck again!! how do you insert an unopend drawing into an open drawing as a block.
This is the code I have so far, mostly obtained from the .net discussion groups .. but it fails at the AddNewlyCreatedObject line with an 'eNotInDatabase' exception. Any ideas greatfully recieved!!
Code:
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports System.IO
<CommandMethod("mmm")> _
Public Sub InsertDrawing()
Dim DwgNm As String = "c:\abc.dwg"
Dim blkNm As String = "abc"
Dim insPt As New Point3d(2, 2, 0)
Dim s3d As New Scale3d(1)
Dim rot As Double = 0
Dim AcDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = AcDoc.Database
Dim AcTrans As Transaction = AcDoc.TransactionManager.StartTransaction
Using AcTrans
Try
Dim id As ObjectId = Nothing
Dim bt As BlockTable = AcTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead, True)
If Not bt.Has(blkNm) Then
Dim srcDB As Database = New Database(False, True)
srcDB.ReadDwgFile(DwgNm, FileShare.Read, False, vbNullString)
id = acCurDb.Insert(DwgNm, srcDB, False)
Dim blk As BlockTableRecord = AcTrans.GetObject(id, OpenMode.ForWrite, False, True)
blk.Name = blkNm
Else
id = bt.Item(blkNm)
End If
Dim btr As BlockTableRecord = AcTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite, True)
Dim bref As BlockReference = New BlockReference(insPt, id)
bref.ScaleFactors = s3d
bref.Rotation = rot
AcTrans.AddNewlyCreatedDBObject(bref, True)
AcTrans.Commit()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Regards Matt
In case you have attributes in the source drawing
it may helps:
Code:
<CommandMethod("IBB", CommandFlags.Session And CommandFlags.Redraw)> _
Public Sub test()
Dim DwgNm As String = "c:\abc.dwg"
Dim blkNm As String = "abc"
Dim insPt As New Point3d(300.0, 300.0, 0.0)
'Dim s3d As New Scale3d(2)
'Dim rot As Double = Math.pi/4
Dim AcDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = AcDoc.Database
InsertDrawingAsBlock(AcDoc, DwgNm, blkNm, insPt)
End Sub
Public Sub InsertDrawingAsBlock(ByVal doc As Document, ByVal filepath As String, ByVal blockname As String, ByVal ipt As Point3d)
Dim curdb As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim loc As DocumentLock = doc.LockDocument()
Using loc
Dim tr As Transaction = doc.TransactionManager.StartTransaction
Using tr
Try
Dim blkid As ObjectId
Dim bt As BlockTable = CType(tr.GetObject(curdb.BlockTableId, OpenMode.ForRead, False), BlockTable)
If bt.Has(blockname) Then
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(String.Format("Block ""{0}"" does already exist" & vbLf & "Try another name", blockname))
Return
End If
Dim btr As BlockTableRecord = CType(tr.GetObject(curdb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim db As Database = New Database(False, True)
Using db
db.ReadDwgFile(filepath, System.IO.FileShare.Read, False, "")
blkid = curdb.Insert(blockname, db, True)
End Using
If (Not bt.Has(blockname)) Then
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(String.Format("Block ""{0}"" does not added to block table" & vbLf & "Exit on Error", blockname))
Return
End If
Dim btrec As BlockTableRecord = CType(tr.GetObject(blkid, OpenMode.ForRead, False), BlockTableRecord)
If String.IsNullOrEmpty(btrec.Name) Then
btrec.UpgradeOpen()
btrec.Name = blockname
btrec.DowngradeOpen()
End If
Dim bref As BlockReference = New BlockReference(ipt, blkid)
Dim mat As Matrix3d = Matrix3d.Identity
bref.TransformBy(mat)
bref.ScaleFactors = New Scale3d(2)
bref.Rotation = Math.PI / 4
btr.AppendEntity(bref)
tr.AddNewlyCreatedDBObject(bref, True)
If btrec.HasAttributeDefinitions Then
Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection
For Each subid As ObjectId In btrec
Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)
Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)
If attDef IsNot Nothing Then
ed.WriteMessage(vbLf & "Value: " + 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.TextStyle = attDef.TextStyle
attRef.Position = attDef.Position + ipt.GetAsVector()
attRef.Tag = attDef.Tag
attRef.FieldLength = attDef.FieldLength
attRef.TextString = attDef.TextString
attRef.AdjustAlignment(curdb)
atcoll.AppendAttribute(attRef)
tr.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End If
ed.UpdateScreen()
tr.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.ToString & vbCr & ex.Message)
End Try
End Using
End Using
End Sub