InsertBlock method

InsertBlock method

Anonymous
Not applicable
2,098 Views
16 Replies
Message 1 of 17

InsertBlock method

Anonymous
Not applicable

Hello,

I have researched this newsgroup and have seen a number of threads 
discussing the problems with the InsertBlock method but none have 
conclusively provided a solution.

It looks like the InsertBlock method works only under some set of ideal 
conditions while in others, it fails with different error messages.

The attached DVB file creates a new block definition and attempts to 
insert it in the current document model space. The error  I am getting 
is "Invalid argument bStrName..."

In another instance, I was able to run the code without problems and the 
InsertBlock method seemed to work, but I did not see the block insert 
object despite a regen, .update, .visible etc.

Any pointers on what code is needed for a smooth run without crashes.

Regards
Rakesh
-- 

AutoCAD customization for Engineering/Mapping/GIS
Get GeoTools @ http://www.4d-technologies.com/geotools
Build MyGeoTools @ http://www.4d-technologies.com/geotools/my_geotools.htm
FREE downloads : http://www.4d-technologies.com/techcenter
Attachment not added (content type not allowed): "bm.dvb"
0 Likes
2,099 Views
16 Replies
Replies (16)
Message 2 of 17

Anonymous
Not applicable
this is wrong: Set bname = blkobj.InsertBlock(Insp, "BOMBLOCK", 1#, 1#, 1#, 0#) should be: set bname = thisDrawing.ModelSpace.InsertBlock(Insp, "BOMBLOCK", 1#, 1#, 1#, 0#) -- Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica (sorry, phony e-mail, SPAM made me do it) "Rakesh Rao" wrote in message news:[email protected]... >
>
> Hello,
>
> I have researched this newsgroup and have seen a number of threads
> discussing the problems with the InsertBlock method but none have
> conclusively provided a solution.
>
> It looks like the InsertBlock method works only under some set of ideal
> conditions while in others, it fails with different error messages.
>
> The attached DVB file creates a new block definition and attempts to
> insert it in the current document model space. The error  I am getting
> is "Invalid argument bStrName..."
>
> In another instance, I was able to run the code without problems and the
> InsertBlock method seemed to work, but I did not see the block insert
> object despite a regen, .update, .visible etc.
>
> Any pointers on what code is needed for a smooth run without crashes.
>
> Regards
> Rakesh
> -- 
>
> AutoCAD customization for Engineering/Mapping/GIS
> Get GeoTools @ http://www.4d-technologies.com/geotools
> Build MyGeoTools @ http://www.4d-technologies.com/geotools/my_geotools.htm
> FREE downloads : http://www.4d-technologies.com/techcenter
> 
>
0 Likes
Message 3 of 17

Dave_B
Enthusiast
Enthusiast
Until someone comes up with a proper solution, you can use this workaround example:

ThisDrawing.SendCommand "-insert C:\Temp\blockname.dwg" & vbCr & "0,0 1 1 1 0" & vbCr

I too struggled with insertblock for several hours before I gave up and used SendCommand.

Since the insertion was the 1st command in my program, I had to put pauses before and after the statement to allow Acad Mechanical 2009 to catch up:

timersub (1),
where timersub is:

Public Sub timersub(PauseTime As Integer)
Dim Start, Finish, TotalTime
'PauseTime = 5 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
'TotalTime = Finish - Start ' Calculate total time.
'MsgBox "Paused for " & TotalTime & " seconds"
End Sub

I also need to put a pause after saveas
0 Likes
Message 4 of 17

Anonymous
Not applicable
Hi Dave,


The code to insert a block with the API is very simple. There is good
sample code in the help files. Why you could spend hours failing to get
it to work is mind boggling.

{code}
ThisDrawing.ModelSpace.InsertBlock Pt, BlockName, XScale, YScale,
ZScale, Angle
{code}

Using it in real program is a bit more complex (as is everything in
programming), but the following code covers most bases. Note that it
uses a Public Type variable "tf" to hold the path to the external block
file and a Public scale variable dgScale

{code}
Function DrawBlock(pPt() As Double, spBlock As String, dX As Double, dY
As Double, dZ As Double) As Boolean
......
On Error Resume Next
Set oBlock = ThisDrawing.ModelSpace.InsertBlock(pPt, spBlock, dX *
dgScale, dY * dgScale, dZ * dgScale, dAzi)
If Err <> 0 Then
'<--- Arrived here because block not already in the drawing
Err.Clear
Dim sTmp As String
Dim sFile As String
sTmp = tF.BlockPath & spBlock & ".dwg"
If FileExists(sTmp) = True Then
Set oBlock = ThisDrawing.ModelSpace.InsertBlock(pPt, sTmp, dX *
dgScale, dY * dgScale, dZ * dgScale, dAzi)
Else
sTmp = "The Block for is not available."
MsgBox sTmp, vbCritical, scMsgHeader
End If
End If
On Error GoTo DBErrorHandler
......
{code}

Use of SendCommand in VBA programming should to be avoided unless there
is no other way of doing the intended task.

Regards,


Laurie Comerford

Dave_B wrote:
> Until someone comes up with a proper solution, you can use this workaround example:
>
> ThisDrawing.SendCommand "-insert C:\Temp\blockname.dwg" & vbCr & "0,0 1 1 1 0" & vbCr
>
> I too struggled with insertblock for several hours before I gave up and used SendCommand.
>
> Since the insertion was the 1st command in my program, I had to put pauses before and after the statement to allow Acad Mechanical 2009 to catch up:
>
> timersub (1),
> where timersub is:
>
> Public Sub timersub(PauseTime As Integer)
> Dim Start, Finish, TotalTime
> 'PauseTime = 5 ' Set duration.
> Start = Timer ' Set start time.
> Do While Timer < Start + PauseTime
> DoEvents ' Yield to other processes.
> Loop
> Finish = Timer ' Set end time.
> 'TotalTime = Finish - Start ' Calculate total time.
> 'MsgBox "Paused for " & TotalTime & " seconds"
> End Sub
>
> I also need to put a pause after saveas
0 Likes
Message 5 of 17

robmatthews
Collaborator
Collaborator
Hi Laurie. (Note: I'm not the OP...)
Thanks for your tip; I'm a noob to Acad VBA, so i appreciate your pointer re insertion of blocks.

One thing; would i be safe to assume that FileExists() is dependant on having the correct reference enabled? That is the one spot where I am having difficulty. I have VisualBasic for Applications, AutoCAD 2005 Type Library and OLE Automation avaliable. What else would i need?
(Using AutoCAD Mech 2005 if that makes any difference)

Thanks,
Rob.
=============================================
This is my signature, not part of my post.
0 Likes
Message 6 of 17

Anonymous
Not applicable
Hi Rob,

FileExists uses a Windows API call. You don't need to specifically load
any references.

Put the following code in your declarations area.

{code}
Declare Function PathFileExists Lib "shlwapi" Alias _
"PathFileExistsA" (ByVal strFile As String) As Long

{code}


And the following code anywhere convenient.

{code}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DESCRIPTION : See if a file exists.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function FileExists(spFileSpec As String) As Boolean
FileExists = PathFileExists(spFileSpec)
End Function ' FileExists
{code}

The blurb I read when I discovered this function indicated it runs about
5 times faster than code like:

{code}
Public Function FileExists(spFileSpec As String) As Boolean
Dim sFileName as string
sFileName = Dir(spFileSpec)
If Len(SFileName) > 0 Then FileExists = True
End Function ' FileExists
{code}


Regards,


Laurie Comerford


RobMatthews wrote:
> Hi Laurie. (Note: I'm not the OP...)
> Thanks for your tip; I'm a noob to Acad VBA, so i appreciate your pointer re insertion of blocks.
>
> One thing; would i be safe to assume that FileExists() is dependant on having the correct reference enabled? That is the one spot where I am having difficulty. I have VisualBasic for Applications, AutoCAD 2005 Type Library and OLE Automation avaliable. What else would i need?
> (Using AutoCAD Mech 2005 if that makes any difference)
>
> Thanks,
> Rob.
0 Likes
Message 7 of 17

robmatthews
Collaborator
Collaborator
Excellent, that's good to know, thanks.
=============================================
This is my signature, not part of my post.
0 Likes
Message 8 of 17

patrick.emin
Alumni
Alumni
We ran into this problem first when we moved from AutoCAD 2005 to AutoCAD 2007. I posted it on forums at the time but never got any explanation. We had to use a workaround, It may not be clearly a bug but it is clearly a difference in how VBA is working between 2005 and 2007, and as such, should have been documented by Autodesk. The problem has been discussed first on french forums: http://www.cadxp.com/modules.php?op=modload&name=XForum&file=viewthread&tid=10925

Patrick Emin animateur de la communauté francophone


Vous avez trouvé un message utile? Alors donnez un "J'aime" à ce message!
Votre question a eu une réponse satisfaisante? Voulez vous avoir l'amabilité de cliquer sur le bouton
 APPROUVER LA SOLUTION  en bas de la réponse qui apporte une solution?
Signez notre Livre d'Or
0 Likes
Message 9 of 17

Dave_B
Enthusiast
Enthusiast
Here's a follow-up to the InsertBlock method bug. The problem occurs frequently with my setup: AutoCAD Mechanical 2009 SP3 when using Objdbx to insert the same block into multiple drawings in the background. It also occurred when I used Acad Mechanical 2004. The VBA program sometimes runs one or more times without error, then Acad crashes. It seems that once Acad "decides" to fatally crash with an enotopenfor write error during an InsertBlock operation, it will continue to do so even after restarting Acad or logging out of Windows XP and back in. However, after restarting my PC I can often run a VBA program sucessfully where it had crashed during earlier attempts. Eventually the fatal crash returns. The workaround I suggested earlier (using SendCommand instead of InsertBlock) did not solve the problem. Acad Mechanical crashed all the same.

I have been running the same program successfully in the vanilla AutoCAD 2009 version (without the AutoCAD Mechanical modules loaded) for the last few weeks, so if anyone is having the same problem, vanilla AutoCAD may be worth a try. Edited by: Dave_B on Dec 16, 2009 10:54 AM
0 Likes
Message 10 of 17

Anonymous
Not applicable
Hi Dave,

SendCommand is not a workaround - it is a kludge.

Do a search of your code and everywhere you find it, write the code to
do the process and most likely you will get rid of your problems.



Regards,


Laurie Comerford


Dave_B wrote:
> Here's a follow-up to the InsertBlock method bug. The problem occurs frequently with my setup: AutoCAD Mechanical 2009 SP3 when using Objdbx to insert the same block into multiple drawings in the background. It also occurred when I used Acad Mechanical 2004. The VBA program sometimes runs one or more times without error, then Acad crashes. It seems that once Acad "decides" to fatally crash with an enotopenfor write error during an InsertBlock operation, it will continue to do so even after restarting Acad or logging out of Windows XP and back in. However, after restarting my PC I can often run a VBA program sucessfully where it had crashed during earlier attempts. Eventually the fatal crash returns. The workaround I suggested earlier (using SendCommand instead of InsertBlock) did not solve the problem. Acad Mechanical crashed all the same.
>
> I have been running the same program successfully in the vanilla AutoCAD 2009 version (without the AutoCAD Mechanical modules loaded) for the last few weeks, so if anyone is having the same problem, vanilla AutoCAD may be worth a try.
>
> Edited by: Dave_B on Dec 16, 2009 10:54 AM
>
0 Likes
Message 11 of 17

Anonymous
Not applicable
If the only way to solve the problem is to restart your system, it means
that the Acad.exe process is not shutting down after using ObjectDBX,
which can happen if an AxDbDocument is not released.

After the problem occurs, use Task Manager to see if there is another
running instance of acad.exe.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:[email protected]...
Here's a follow-up to the InsertBlock method bug. The problem occurs
frequently with my setup: AutoCAD Mechanical 2009 SP3 when using Objdbx to
insert the same block into multiple drawings in the background. It also
occurred when I used Acad Mechanical 2004. The VBA program sometimes runs
one or more times without error, then Acad crashes. It seems that once Acad
"decides" to fatally crash with an enotopenfor write error during an
InsertBlock operation, it will continue to do so even after restarting Acad
or logging out of Windows XP and back in. However, after restarting my PC I
can often run a VBA program sucessfully where it had crashed during earlier
attempts. Eventually the fatal crash returns. The workaround I suggested
earlier (using SendCommand instead of InsertBlock) did not solve the
problem
. Acad Mechanical crashed all the same.

I have been running the same program successfully in the vanilla AutoCAD
2009 version (without the AutoCAD Mechanical modules loaded) for the last
few weeks, so if anyone is having the same problem, vanilla AutoCAD may be
worth a try.

Edited by: Dave_B on Dec 16, 2009 10:54 AM
0 Likes
Message 12 of 17

Dave_B
Enthusiast
Enthusiast
Tony,

I saw your post today. Thank you for taking the time to read my post and respond. The problem is consistently occurring when I run my program in Acad Mechanical, and it does not occur when I run it in vanilla Acad. I do not see any extra acad.exe processes running in Task Manager.

Here is how I call objdbx and loop through all the drawings. Do you know if I need to do anything besides Set moDbxDoc = Nothing to release the AxDbDocument?

{code}
Dim moDbxDoc As AXDBLib.AxDbDocument

di1 = 1: di = UBound(dwglistar) + 1
If di = 1 Then di1 = 0
Do While di1 <= di

Set moDbxDoc = AutoCAD.AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument.17")
....
CurrentFile = dwglistar(di1)

moDbxDoc.Open (CurrentFileandPath)
di1 = di1 + 1
...
Set moDbxDoc = Nothing
...
Loop
{code}
0 Likes
Message 13 of 17

Anonymous
Not applicable
What I meant was that after you close the only running instance of AutoCAD
(after running your code), check to see if it is still running in task manager
(on the processes tab).

The only thing I can suggest is to try:

System.Runtime.InteropServices.Marshal.FinalReleaseComObject( dbxdoc )

Do this just before you set the variable assigned to the AxDbDocument to
Nothing.

But, my suspicion is that something's going on in AMECH that's causing this, and
would not be terribly suprised if it turned out to be yet another case of an
Autodesk vertical stepping on end user customization.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:[email protected]...
Tony,

I saw your post today. Thank you for taking the time to read my post and
respond. The problem is consistently occurring when I run my program in Acad
Mechanical, and it does not occur when I run it in vanilla Acad. I do not see
any extra acad.exe processes running in Task Manager.

Here is how I call objdbx and loop through all the drawings. Do you know if I
need to do anything besides Set moDbxDoc = Nothing to release the AxDbDocument?

{code}
Dim moDbxDoc As AXDBLib.AxDbDocument

di1 = 1: di = UBound(dwglistar) + 1
If di = 1 Then di1 = 0
Do While di1 <= di

Set moDbxDoc =
AutoCAD.AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument.17")
....
CurrentFile = dwglistar(di1)

moDbxDoc.Open (CurrentFileandPath)
di1 = di1 + 1
...
Set moDbxDoc = Nothing
...
Loop
{code}
0 Likes
Message 14 of 17

Dave_B
Enthusiast
Enthusiast
Tony,

Thanks for your suggestion, but it didn't help. At least I can run my insertblock programs in vanilla Acad. Unfortunately, everyone else in my office uses Acad Mech or Electrical :(.
0 Likes
Message 15 of 17

Anonymous
Not applicable
I have also been having this problem in C3D 2008. First insert works, and second gets a filer error. So it might be problem in the verticals.

I have followed the suggestion by kce in http://discussion.autodesk.com/forums/thread.jspa?messageID=6201292鿌 to search the file paths manually and that seems to be working.
0 Likes
Message 16 of 17

Dave_B
Enthusiast
Enthusiast
Cathy,

Thanks for your suggestion, but I have tried the findfile method before a while back with no improvement. VBA isn't having any trouble finding the file to insert. I think insertblock works fine in acad vba but not ObjDBX.

However, I am on to something! I can now run my ObjDBX multiple block insertion programs successfully without crash in Acad Mechanical 2009 after I discovered the following phenomenon by trial and error:

One thing I didn't mention in my other posts (because I didn't think it could possibly be related) was that at our company we routinely save drawings as type AutoCAD 2004 (vanilla). This is so companies requiring drawings but do not have Acad 2007 or higher can open the files, and also because until recently our PDM could not handle anything later than AutoCAD 2004. If I open a drawing in either Acad Mechanical or Acad vanilla and save it as type AutoCAD (any year), close it, then try to run my ObjDBX on the file to insert a block into the drawing, it will crash Acad Mechanical , but it will not crash vanilla Acad. However, if I open the same file in AutoCAD Mechanical, save it as type AutoCAD Mechanical any year (2004-2009), close it and then run my ObjDBX on the file to insert a block into the drawing, my programs run without crashing. It's strange because when stepping through the code, the crash occurs at the InsertBlock line, not during the save.

It doesn't seem to matter which version the drawing being inserted was saved as. So this phenomenon could explain why vanilla Acad appeared to be more forgiving than Acad Mech. More importantly, it could help Autodesk fix this ObjDBX Insertblock bug. Or maybe or someone on this site could develop a workaround (I tried SaveAs (path & filename, acnative) before the InsertBlock, but the program crashed at this point; I don't know the correct file type to use for Acad Mechanical). Or I could ensure beforehand that the files are always saved as type Acad Mechanical until needed in another format.
0 Likes
Message 17 of 17

Anonymous
Not applicable
Get out out timers use the following:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Example
Sub Main
Sleep(500)' Milliseconds
On other thing!!!
Sounds like you have a onerror statment there
(DO NOT) use err.clear
Endsub
0 Likes