I'm using AutoCAD 2013 and I want to import block from another file. I wrote the code below:

Dim Zero(0 To 2) As Double
Dim i As Integer
For i = 0 To 2
    Zero(i) = 0
Next i

Dim BlockRef As AcadBlockReference
FileToInsert = "D:\blocks.dwg"
Set BlockRef = ThisDrawing.ModelSpace.InsertBlock(Zero, FileToInsert, 1#, 1#, 1#, 0)

but I get the following error:

Run-time error '-2145386390 (8020006a)'
No database

I find it very strange because the same code worked a week ago and I did't touch none of the files since then... If I open blocks.dwg there are a few blocks in it, just as they should.

If I comment out this part of the code, the same error seems to move to the next

Set ... = ThisDrawing.ModelSpace. ...

I would very much appreciate your help, Špela

有帮助吗?

解决方案

If the drawing already contains the block, you need to call InsertBlock with the block name only. Something like this:

Dim exist as Boolean
exist = False
For i = 0 To ThisDrawing.Blocks.Count - 1
  If StrComp(ThisDrawing.Blocks.Item(i).Name, "blocks", vbTextCompare) = 0  Then
    exist = True
    Exit For
  End If  
Next i
ThisDrawing.ModelSpace.InsertBlock(point, IIf(exist, "blocks", "D:\blocks.dwg"), 1, 1, 1, 0)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top