Jump to content

Insert Block from within Drawing - VBA


hairyuga

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...