Jump to content

Insert Block from within Drawing - VBA


Recommended Posts

Posted

I'm getting an error "Argument not Optional" for InsertBlock when I try to insert a block from within the drawing. I don't get it!

 

'Create Panel
Private Sub cmdCreatePanel_Click()
Dim varPick As Variant
Dim dblLength As Double
Dim dblWidth As Double
Dim dblHeight As Double
Dim dblCenter(2) As Double
Dim objEnt As Acad3DSolid
Dim InsertionPoint As Variant
Dim gpartref As McadPartReference
Dim ProfileRef As AcadBlockReference


Me.Hide

'get the input from user
With ThisDrawing.Utility
.InitializeUserInput 1
varPick = .GetPoint(, vbCr & "Pick a corner point: ")
.InitializeUserInput 1 + 2 + 4, ""
dblLength = .DistanceToReal(cmbPanelLength.Text, acEngineering)
.InitializeUserInput 1 + 2 + 4, ""
dblWidth = .DistanceToReal(cmbPanelWidth.Text, acEngineering)
.InitializeUserInput 1 + 2 + 4, ""
dblHeight = .DistanceToReal(cmbPanelHeight.Text, acEngineering)
' Return the current value of the insertion point
InsertionPoint = varPick
End With
'calculate center point from input
dblCenter(0) = CDbl(varPick(0)) + (dblLength / 2)
dblCenter(1) = CDbl(varPick(1)) + (dblWidth / 2)
dblCenter(2) = CDbl(varPick(2)) + (dblHeight / 2)

'draw the entity
Set objEnt = ThisDrawing.ModelSpace.AddBox(dblCenter, dblLength, _
dblWidth, dblHeight)
'get part reference
Set gpartref = ThisDrawing.ModelSpace.AddCustomObject("AcmPartRef")
gpartref.Origin = varPick

'insert Profile Block
InsertionPoint = varPick
Set ProfileRef = ThisDrawing.ModelSpace.InsertBlock(InsertionPoint, "P+P", varPick(0), varPick(1), varPick(2))

'update entity
objEnt.History = True
objEnt.Update
ThisDrawing.Regen acActiveViewport
Me.Show
End Su

Posted

You will need to include a rotation argument in your insertblock expression - this argument is "not optional"

Posted

I know almost nothing about VBA, but try this:

 

'Create Panel
Private Sub cmdCreatePanel_Click()
Dim varPick As Variant
Dim dblLength As Double
Dim dblWidth As Double
Dim dblHeight As Double
Dim dblCenter(2) As Double
Dim objEnt As Acad3DSolid
Dim InsertionPoint As Variant
Dim gpartref As McadPartReference
Dim ProfileRef As AcadBlockReference


Me.Hide

'get the input from user
With ThisDrawing.Utility
.InitializeUserInput 1
varPick = .GetPoint(, vbCr & "Pick a corner point: ")
.InitializeUserInput 1 + 2 + 4, ""
dblLength = .DistanceToReal(cmbPanelLength.Text, acEngineering)
.InitializeUserInput 1 + 2 + 4, ""
dblWidth = .DistanceToReal(cmbPanelWidth.Text, acEngineering)
.InitializeUserInput 1 + 2 + 4, ""
dblHeight = .DistanceToReal(cmbPanelHeight.Text, acEngineering)
' Return the current value of the insertion point
InsertionPoint = varPick
End With
'calculate center point from input
dblCenter(0) = CDbl(varPick(0)) + (dblLength / 2)
dblCenter(1) = CDbl(varPick(1)) + (dblWidth / 2)
dblCenter(2) = CDbl(varPick(2)) + (dblHeight / 2)

'draw the entity
Set objEnt = ThisDrawing.ModelSpace.AddBox(dblCenter, dblLength, _
dblWidth, dblHeight)
'get part reference
Set gpartref = ThisDrawing.ModelSpace.AddCustomObject("AcmPartRef")
gpartref.Origin = varPick

'insert Profile Block
InsertionPoint = varPick
Set ProfileRef = ThisDrawing.ModelSpace.InsertBlock(InsertionPoint, "P+P", varPick(0), varPick(1), varPick(2), 0)

'update entity
objEnt.History = True
objEnt.Update
ThisDrawing.Regen acActiveViewport
Me.Show
End Sub

Posted

Forgot about Rotation :)

 

Almost! Now I get an Invalid Z arguement!

Posted

I don't know enough VBA to get you out of that one :)

 

Maybe in LISP I could do it, but not VBA :oops:

Posted

Just out of curiosity, why are you doing it in VBA? This task would be much easier to accomplish using LISP...

Posted

I want to learn VBA so I'm trying to accomplish it in that language but Iam new at this programming stuff.

Posted

Yes, I've read that thread. Right now, most programs in my office are written in VBA (before I got here), hence the need to learn it. Even if VBA dies, Autodesk will support it for years to come before they phase it out because of older users.

Posted

I can see your reasoning, I just find that things can be accomplished easier and with a lot less coding in LISP :P

Posted

VBA is my preferred choice of language. Possibly as I was trained in FORTRAN which isn't too dissimilar to BASIC which in turn isn't too dissimilar to VB which etc. Also I find it MUCH easier to read than LISP. There are some things you can't do in VBA but once you have a set of sub routine written it doesn't take too long to put it all together. Unfortunately I can't help here as our new IT policy severly limits my time around the forum at work and I don't have AutoCAD at home.

Posted

I suppose its just what you get used to Dave, I have only really ever played around with LISP and so when I look at VBA, it just looks too complicated and I haven't got around to dissecting it and studying it as yet...

 

Am I right in saying that Fortran was one of the first programming languages to be brought into existence?

Posted

The InsertBlock method has this signature:

 

RetVal = object.InsertBlock(InsertionPoint, Name, Xscale, Yscale, ZScale, Rotation [, Password])

 

 

Your code is trying to use the x, y, and z coordinates of the original .GetPoint call for the scale values. I suppose that may be what you want – but it would be out of the ordinary.

 

The reason you are getting the “Invalid Z argument” is because the return from the .GetPoint probably has a z = 0, or maybe negative. Neither work with block insertion.

Posted
Am I right in saying that Fortran was one of the first programming languages to be brought into existence?
http://en.wikipedia.org/wiki/Fortran

looks that way. I thought I had been taught FORTRAN IV but looking at the dates it was probably a latter version. I learned sometime in the 70s. We were looking at getting a computer to do finite element analysis rather than getting the analysis done by a bureaux in London. At about the same time Commodore brought out their "PET" machine - 1k memory, tape drive tiny keyboard and built in BASIC language so I didn't use FORTRAN much and quickly became OK at BASIC.

Posted

It still amazes me how much computers have evolved in such a short space of time...

 

Ibm704.gif

 

Thanks for the info Dave.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...