Hi Yuan,
All I want is to insert an external drawing into the drawing in which the code is executed. BarChairD.Dwg is the external file. It has an attribute which needs to be modified. The drawing with the Mtext is where the block needs to be inserted.
Please see my comments inserted into your text below. I have got a cue from your reply and modified the code. I will post the code including the Mtext part which was not there earlier. Now the barchairD drawing is inserted in the Mtext drawing, but it is not inserted at the specific point p1 specified separately. Now I think it is only a matter of some minor tweaks.
Regards,
Anish
'' Select profile text
Dim prTSSet As PromptSelectionResult
prTSSet = ed.SelectCrossingWindow(New Point3d(XprT1, YprT1, 0), New Point3d(XprT2, YprT2, 0), acSelFtrprT)
Dim acSSetPrT As SelectionSet = prTSSet.Value
Dim Tso As SelectedObject = acSSetPrT.Item(0)
If prTSSet.Status = PromptStatus.OK Then
Using acTrans3 As Transaction = acCurDb3.TransactionManager.StartTransaction()
Dim acBlkTblTxt As MText
acBlkTblTxt = CType(acTrans3.GetObject(Tso.ObjectId, OpenMode.ForRead), MText)
MsgBox(acBlkTblTxt.Text)
'Start a transaction
Using blkDb As Database = New Database(False, True)
blkDb.ReadDwgFile("C:\Base\BarChairD.dwg", IO.FileShare.Read, False, "")
Using tr1 As Transaction = blkDb.TransactionManager.StartTransaction()
Dim BlkName As String = SymbolUtilityServices.GetBlockNameFromInsertPathName("C:\Base\BarChairD.dwg")
Dim id As ObjectId = acCurDb3.Insert(BlkName, blkDb, False)
If id.IsNull Then
ed.WriteMessage(vbLf & "Failed to insert block")
Return
End If
'Open the Block table record Model space for write
Dim acBlkTblRecbc As BlockTableRecord = CType(tr1.GetObject(blkDb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim br As New BlockReference(p1, id)
'' Add the new object to the block table record and the transaction
acBlkTblRecbc.AppendEntity(br)
br.SetDatabaseDefaults()
tr1.AddNewlyCreatedDBObject(br, True)
tr1.Commit()
End Using
' blkDb.SaveAs("C:\Base\BarChairD", DwgVersion.Current)
End Using
acTrans3.Commit()
End Using
According to what your described, it seems to me, you want to insert a block DWG file into another drawing file, which is not currently opened in AutoCAD editor- The drawing with Mtext is open (or it is already opened in AutoCAD editor). It sounds like there should have 2 drawings (or 2 databases) involved: one has an MTEXT in it as target drawing to be processed; and the other one as block file to be inserted into the former-correct. However, your code logic does not indicate it is that case and only one drawing/database is dealt with by the code.
What your code does is:
1. You create an empty side database and read the file "C:\Base\BarChairD.dwg" into it. Now the question: is this database/dwg file is the one that has said MText in it and you want to insert a block into it-No, this is the block file?
2. The strange thing after the side database is created: you insert the side database into ITSELF(!!!) by this line of code, which also has the third parameter passed in as "True" (if a external file being inserted as Block, it should be "False")
Ok, i changed it to false, but it does not solve the problem
Dim id As ObjectId = blkDb.Insert(BlkName, blkDb, True)
3. Eventually you save the side database back as "....\BarChairD.dwg". So is the database/file is the block file, or is the actual target drawing you want to insert a block into it?
This was suggested by Giles when I said that the block is not getting inserted. I am not sure what it does.
So, what is you really want to do?