CREATE A BLOCK-WHERE AM I GOING WRONG?

CREATE A BLOCK-WHERE AM I GOING WRONG?

Anonymous
Not applicable
386 Views
7 Replies
Message 1 of 8

CREATE A BLOCK-WHERE AM I GOING WRONG?

Anonymous
Not applicable
I WANT TO CREATE A BLOCK WHICH IS BASICALLY A CROSSHAIRS, SAY 2 UNITS ACROSS, 2 UNITS UP. I KEEP GETTING THE ERROR MESSAGE "INVALID ARGUMENT NAME IN ADD METHOD".

I'VE SPENT SEVERAL HOURS TRYING VARIOUS THINGS, AND HAVE STUDIED JOE SUPHTIN BOOK P258, AND MARION COTTINGHAM BOOK P491 ON BLOCKS, AND STILL I CAN'T GET THE MACRO TO WORK.

PLEASE CAN SOMEONE HELP BY SHOWING ME WHY IT WON'T WORK.

THANKS.


Option Explicit

Public acad As Object

Sub Draw_()

Dim dwg As Object

Dim acad As New AutoCAD.AcadApplication

Dim crosshair As String

Dim Line1 As AcadLine

Dim Line2 As AcadLine

Dim blockobject As AcadBlock

Dim origin(0 To 2) As Double

Dim StartLine1(0 To 2) As Double

Dim EndLine1(0 To 2) As Double

Dim StartLine2(0 To 2) As Double

Dim EndLine2(0 To 2) As Double

acad.Visible = True

origin(0) = 5: origin(1) = 5: origin(2) = 5


StartLine1(0) = 0
StartLine1(1) = 0
StartLine1(2) = 0
EndLine1(0) = 2
EndLine1(1) = 0
EndLine1(2) = 0

StartLine2(0) = 1
StartLine2(1) = -1
StartLine2(2) = 0
EndLine2(0) = 1
EndLine2(1) = 1
EndLine2(2) = 0

Set dwg = acad.ActiveDocument

dwg.Layers.Add ("geometry")

Set blockobject = ThisDrawing.Blocks.Add(origin, crosshair)

blockobject.AddLine StartLine1, EndLine1

blockobject.AddLine StartLine2, EndLine2

ZoomExtents

End Sub
0 Likes
387 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
THIS IS NOT SOMEONE SENDING IN AN ANSWER, IT'S ME PASTING IN THE CODE AGAIN, BECAUSE IT HAS COME OUT JOINED UP, AND NOT IN SINGLE LINE FORMAT AS IT LOOKED WHEN INITIALLY PASTED IN AND POSTED. STRANGE WHY IT IS DOING THIS?, AND CONSEQUENTLY MAKING THE CODE HARDER TO READ. HERE GOES: (IF IT JOINS CODE UP AGAIN, THEN I DON'T WHY!)



Option Explicit
Public acad As Object

Sub Draw_()

Dim dwg As Object
Dim acad As New AutoCAD.AcadApplication
Dim crosshair As String
Dim Line1 As AcadLine
Dim Line2 As AcadLine
Dim blockobject As AcadBlock
Dim origin(0 To 2) As Double
Dim StartLine1(0 To 2) As Double
Dim EndLine1(0 To 2) As Double
Dim StartLine2(0 To 2) As Double
Dim EndLine2(0 To 2) As Double
acad.Visible = True
origin(0) = 5: origin(1) = 5: origin(2) = 5


StartLine1(0) = 0
StartLine1(1) = 0
StartLine1(2) = 0
EndLine1(0) = 2
EndLine1(1) = 0
EndLine1(2) = 0

StartLine2(0) = 1
StartLine2(1) = -1
StartLine2(2) = 0
EndLine2(0) = 1
EndLine2(1) = 1
EndLine2(2) = 0

Set dwg = acad.ActiveDocument
dwg.Layers.Add ("geometry")
Set blockobject = ThisDrawing.Blocks.Add(origin, crosshair)

blockobject.AddLine StartLine1, EndLine1
blockobject.AddLine StartLine2, EndLine2

ZoomExtents
End Sub
0 Likes
Message 3 of 8

Anonymous
Not applicable
I had the same problem.

To get a carriage return I type BR surrounded by <>.

Use the test forum to see if it works.

See my attached file for the text I used to post this.


Regards - Nathan

0 Likes
Message 4 of 8

Anonymous
Not applicable
PLEASE STOP SHOUTING! MY EYES HURT.

Are you attempting this in VBA?


Sub Test()

' A string variable is not strictly req'd,
' it's here as a sample.
Dim BlockName As String
BlockName = "Sample"
Dim LayerName As String
LayerName = "Geometry"

' Make point array for block's insertion point
Dim ptOrigin(2) As Double
ptOrigin(0) = 0#: ptOrigin(1) = 0#: ptOrigin(2) = 0#

' Create the block object
Dim Block As AcadBlock
Set Block = ThisDrawing.Blocks.Add(ptOrigin, BlockName)

' Make point arrays for lines
Dim pt1(2) As Double
pt1(0) = -1#: pt1(1) = 0#: pt1(2) = 0#
Dim pt2(2) As Double
pt2(0) = 1#: pt2(1) = 0#: pt2(2) = 0#
Dim pt3(2) As Double
pt3(0) = 0#: pt3(1) = -1#: pt3(2) = 0#
Dim pt4(2) As Double
pt4(0) = 0#: pt4(1) = 1#: pt4(2) = 0#

' Add the lines to the block's definition
Block.AddLine pt1, pt2
Block.AddLine pt3, pt4

' Make point array for block reference's insertion
Dim ptIns(2) As Double
ptIns(0) = 5#: ptIns(1) = 5#: ptIns(2) = 0#

' Make sure layer exists
ThisDrawing.Layers.Add (LayerName)

' Insert the block
Dim BlockRef As AcadBlockReference
Set BlockRef = ThisDrawing.ModelSpace.InsertBlock(ptIns, BlockName, 1#,
1#, 1#, 0#)

BlockRef.Layer = LayerName
ThisDrawing.Application.ZoomExtents

End Sub



--
R. Robert Bell, MCSE
www.AcadX.com


"mostyn" wrote in message
news:f160d5d.0@WebX.maYIadrTaRb...
| THIS IS NOT SOMEONE SENDING IN AN ANSWER, IT'S ME PASTING IN THE CODE
AGAIN, BECAUSE IT HAS COME OUT JOINED UP, AND NOT IN SINGLE LINE FORMAT AS
IT LOOKED WHEN INITIALLY PASTED IN AND POSTED. STRANGE WHY IT IS DOING
THIS?, AND CONSEQUENTLY MAKING THE CODE HARDER TO READ. HERE GOES: (IF IT
JOINS CODE UP AGAIN, THEN I DON'T WHY!)
|
|
| Option Explicit
| Public acad As Object
|
| Sub Draw_()
|
| Dim dwg As Object
| Dim acad As New AutoCAD.AcadApplication
| Dim crosshair As String
| Dim Line1 As AcadLine
| Dim Line2 As AcadLine
| Dim blockobject As AcadBlock
| Dim origin(0 To 2) As Double
| Dim StartLine1(0 To 2) As Double
| Dim EndLine1(0 To 2) As Double
| Dim StartLine2(0 To 2) As Double
| Dim EndLine2(0 To 2) As Double
| acad.Visible = True
| origin(0) = 5: origin(1) = 5: origin(2) = 5
|
|
| StartLine1(0) = 0
| StartLine1(1) = 0
| StartLine1(2) = 0
| EndLine1(0) = 2
| EndLine1(1) = 0
| EndLine1(2) = 0
|
| StartLine2(0) = 1
| StartLine2(1) = -1
| StartLine2(2) = 0
| EndLine2(0) = 1
| EndLine2(1) = 1
| EndLine2(2) = 0
|
| Set dwg = acad.ActiveDocument
| dwg.Layers.Add ("geometry")
| Set blockobject = ThisDrawing.Blocks.Add(origin, crosshair)
|
| blockobject.AddLine StartLine1, EndLine1
| blockobject.AddLine StartLine2, EndLine2
|
| ZoomExtents
| End Sub
|
0 Likes
Message 5 of 8

Anonymous
Not applicable
Thanks Nathan for that bit of advice. I'll try it on my next question.
0 Likes
Message 6 of 8

Anonymous
Not applicable
Thanks Robert for that, it worked. Gives you a buz when you've spent ages trying something, then someone helps and it all works. Appreciated.

Terry
0 Likes
Message 7 of 8

Anonymous
Not applicable
You never assigned a value to the crosshair variable but you're passing it to the Blocks.Add function as the block name.

Sub Draw_()
Dim dwg As Object
Dim acad As New AutoCAD.AcadApplication
Dim crosshair As String
Dim Line1 As AcadLine
Dim Line2 As AcadLine
Dim blockobject As AcadBlock
Dim origin(0 To 2) As Double
Dim StartLine1(0 To 2) As Double
Dim EndLine1(0 To 2) As Double
Dim StartLine2(0 To 2) As Double
Dim EndLine2(0 To 2) As Double

acad.Visible = True
origin(0) = 5: origin(1) = 5: origin(2) = 5
StartLine1(0) = 0
StartLine1(1) = 0
StartLine1(2) = 0
EndLine1(0) = 2
EndLine1(1) = 0
EndLine1(2) = 0
StartLine2(0) = 1
StartLine2(1) = -1
StartLine2(2) = 0
EndLine2(0) = 1
EndLine2(1) = 1
EndLine2(2) = 0
Set dwg = acad.ActiveDocument dwg.Layers.Add ("geometry")
Set blockobject = ThisDrawing.Blocks.Add(origin, crosshair) '<---- Problem is here
blockobject.AddLine StartLine1, EndLine1
blockobject.AddLine StartLine2, EndLine2
ZoomExtents
End Sub


You could simplify this a little:
0 Likes
Message 8 of 8

Anonymous
Not applicable
Happy to help.

--
R. Robert Bell, MCSE
www.AcadX.com


"mostyn" wrote in message
news:f160d5d.4@WebX.maYIadrTaRb...
| Thanks Robert for that, it worked. Gives you a buz when you've spent ages
trying something, then someone helps and it all works. Appreciated.
| Terry
|
0 Likes