As stated, when I use VB.NET to insert a block it has no name, thus making it behave oddly. Is there a way to define the block name properly since it apparently doesn't use the name of the file I am importing? I have tried defining the block manually and have the same result so my code must be missing something:
Public Function InsertBlock(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal expl As Boolean, ByVal rotation As Double, ByVal schaal As Integer) As ObjectId If Not File.Exists(BlockName) Then MsgBox("File does not exist?") Return ObjectId.Null End If Dim myDB As Database Dim myDwg As Document myDwg = Application.DocumentManager.MdiActiveDocument Using docloc As DocumentLock = myDwg.LockDocument myDB = myDwg.Database Dim ed As Editor = myDwg.Editor Using tr As Transaction = myDwg.TransactionManager.StartTransaction Try Using db As Database = New Database(False, True) db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "") Dim BlkId As ObjectId BlkId = myDB.Insert(BlockName, db, True) Dim bt As BlockTable = tr.GetObject(myDB.BlockTableId, OpenMode.ForRead, True) Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True) Dim bref As BlockReference = New BlockReference(InsPt, BlkId) bref.Rotation = rotation bref.ScaleFactors = New Scale3d(schaal, schaal, schaal) btr.AppendEntity(bref) tr.AddNewlyCreatedDBObject(bref, True) If expl Then bref.ExplodeToOwnerSpace() bref.Erase() End If tr.Commit() End Using Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.ToString) End Try End Using End Using End Function
Thanks for any tips that may be offered!
Solved! Go to Solution.
Just at the quick glance,
Exploded objects does not appends in the database automatically,
so you need to do it within the code after exploding, iow:
for each exploded obj
That's not needed when using ExplodeToOwnerSpace().
Looking at your code I suppose the boolean parameter 'expl' is true when calling 'InsertBlock'?
I'd suggest you do a regular 'INSERT' after you ran your command, if 'BlockName' is among the block definitions you only need to set the 'expl' parameter false in your call.
This is mostly code I copied from sources online, so the expl function is never really used to explode. I typically set the value to false whenever I insert a block.
Do you think elimination of this expl function entirely would help solve my problem? The result I end up with is an attributed block, but one which can not be edited due to what looks like is the fact that the block has no name when listed.
Your problem (block inserted without name, or should I say, with a "blank name") is due to the fact that you use a fully pathed file name as block name.
In your code, the parameter BlockName you passed in is obviously a block file name with full path (since you use File.Exists() to verify the block file's existance), it must looks like something "C:\.....\myblock.dwg". Such a string value contains invalid characters that are not allowed to be in block name, such as ":", "\"...
At this line of your code:
you passed a fully pathed file name as a block name for the inserted DB (block). Somehow, instead of raising an exception due to invalid character, the API method still inserts the block and give it a blank name.
If you manually create a block in Acad, and try to give it name like "C:\Folder\TheBlk.dwg", Acad would prompt you the invalid characters in block name.
So, in your case, you need to supply a valid block name like this:
That is, it is your responsibility to make sure the block name you supplied in the Database.Insert() method contains only valid characters, or Acad would make a blank-named block.
BTW, sometime, before inserting a block froom a block drawing file, it may be better to check if a block with the same name has already existed in the targeting database. inserting block drawing file and name it as the same name of an existing block will redefine the existing block definition, which may or may not be the intented thing to do.
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register
Start with some of our most frequented solutions to get help installing your software.