Visual basic

Visual basic

zsolti_sdd
Enthusiast Enthusiast
1,692 Views
10 Replies
Message 1 of 11

Visual basic

zsolti_sdd
Enthusiast
Enthusiast

Hi

 

how can i create an offset to o polyline(or line).( rectangle polyline: points p1, p2 p3,p4)

 

te code for the rectangle is :

 

Private Sub CommandButton1_Click()

pi = 3.14
UserForm1.Hide

pt1 = ThisDrawing.Utility.GetPoint(, "left down corner:")
pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))

ThisDrawing.ModelSpace.AddLine pt1, pt2
ThisDrawing.ModelSpace.AddLine pt2, pt3
ThisDrawing.ModelSpace.AddLine pt3, pt4
ThisDrawing.ModelSpace.AddLine pt4, pt1

 

End Sub

 

I NEED THE OFFSET () CODE FOR THE LINES

0 Likes
Accepted solutions (1)
1,693 Views
10 Replies
Replies (10)
Message 2 of 11

Norman_Yuan
Mentor
Mentor

Well, you ask "offset" to a polyline (assume it is AcadLWPolyline, but your code create line (AcadLine). The way to do offset to them could be different.

 

To do "offset" to line, since you know the line's start/end points, it is not terribly difficult to do a bit math to find start/end points of its offset line.

 

To do "offset" to polyline, the math could be a bit tricky/difficult depending on the polyline's shape. Furtunately we do not have to do the calculation in the case of polyline. you can call

 

AcadLWPolyline.Offset() method to create the "offset" for you.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 11

zsolti_sdd
Enthusiast
Enthusiast

 

help me to create de rectangle with polyline.

 

Private Sub CommandButton1_Click()
pi = 3.14
UserForm1.Hide

pt1 = ThisDrawing.Utility.GetPoint(, "left down corner")
pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))

 

ThisDrawing.ModelSpace.AcadLWPolyline pt1, pt2

ThisDrawing.ModelSpace.AcadLWPolyline  pt2, pt3
ThisDrawing.ModelSpace.AcadLWPolyline pt3, pt4
ThisDrawing.ModelSpace.AcadLWPolyline pt4, pt1

 

 

NOT WORKING , WHY ????

 

when i creat the rectangle (polyline), i want to do an offset to the rectangle. and calculate the area.

 

thanks

0 Likes
Message 4 of 11

Norman_Yuan
Mentor
Mentor

You may need to do a bit more study in AutoCAD to know what polyline (AcadLWPoline) is.

 

AcadModelSpace.AddLightWeigthPolyline() needs to be supplied by a one-dimension array of Double type, not 2 points. Also, to create a rectangle in polyline, you create ONE polyline (calling AddLightWeightPolyline() once) with 4 verices/points, not 4 times.

 

You can find out how to create polyline and how to "offset" it:

 

1. Go to AutoCAD's VBA editor;

2. Make "Object Browser" window appear

3. select "AcadModelSpace" on the left side and select "AddLightWeightPolyline" on the right side, hit "?" on top of the Object Bowser window, which brings you to VBA document and give you a link to VBA code example of how to call this method

4. Again, in the Object Browser window. select AcadLWPolyline on the left side, select "Offset" on the right side, hit "?" leads you to code example of how to use AcadLWPolyline.Offset() method.

5. Do use Object Browser to learn more about AutoCAD COM object model

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 11

zsolti_sdd
Enthusiast
Enthusiast

not working

what is vertices list???

0 Likes
Message 6 of 11

zsolti_sdd
Enthusiast
Enthusiast
ThisDrawing.ModelSpace.AcadLWPolyline pt1, pt2, pt3, pt4
0 Likes
Message 7 of 11

zsolti_sdd
Enthusiast
Enthusiast

i want to do this in vba.

 

draw a simple window

 

4 points pt1--pt4

offsets 50 and 25.

hatches

and the opening  for window dashed lines

 

Window.png attachment

 

thanks

0 Likes
Message 8 of 11

Norman_Yuan
Mentor
Mentor

If you studied VBA document and did these:

 

1. go to VBA IDE;

2. open Object Borwser;

3. find AcadModelSpace.AddLWPolyline;

4. click "?" on top of the Object Browser window);

 

you would have known how to call AcadLWPolyline() with detailed example.

 

Take the picture you attached as an example, to drawiing the outmost rectangle (assume its lower-left corner is at 0,0), you do:

 

Dim coords(0 to 7) As Double

coords[0]=0.0

coords[1]=0.0

coords[2]=2500.0

coords[3]=0.0

coords[4]=2500.0

coords[5]=1500.0

coords[6]=0.0

coords[7]=1500.0

 

Dim rect As AcadLWPolyline

Set rect=ThisDocument.ModelSpace.AddLWPolyline(coords)

rect.Closed=True

rect.Update

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 11

zsolti_sdd
Enthusiast
Enthusiast

isn1t working

0 Likes
Message 10 of 11

Norman_Yuan
Mentor
Mentor
Accepted solution

Well, the answer I post was just off my head and was not actually written abd tested in AutoCAD VBA editor. If you repeated the code in AutoCAD VBA editor, it would have prompted the obvious syntext error, such as

 

coords[0]

 

should have been

 

coords(0)

 

Then you should get the idea.

 

Anyway, here is the actual code written in AutoCAD VBA editor and should be working:

 

Option Explicit

Public Sub Test()

    Dim coords(0 To 7) As Double
    coords(0) = 0#
    coords(1) = 0#
    coords(2) = 2500#
    coords(3) = 0#
    coords(4) = 2500#
    coords(5) = 1500#
    coords(6) = 0#
    coords(7) = 1500#

    Dim rect As AcadLWPolyline
    Set rect = ThisDrawing.ModelSpace.AddLightWeightPolyline(coords)
    rect.Closed = True
    rect.Update

End Sub

Norman Yuan

Drive CAD With Code

EESignature

Message 11 of 11

zsolti_sdd
Enthusiast
Enthusiast

THANKS

 

it working. the problem was the brackets ()  Smiley Very Happy

 

How can i do to read the value from coords(0) from a textbox.???

0 Likes