Author Topic: Do polylines only work with arrays?  (Read 2864 times)

0 Members and 1 Guest are viewing this topic.

Co.Mnstr

  • Guest
Do polylines only work with arrays?
« on: October 07, 2008, 02:27:01 PM »
So I'm new to the programming idea. I've done projects in LISP, and customized my share of pallet commands. But now I want to take some time to understand how to do the more detailed things that Autocad is capable of. So I started out with the simple task...

I want to select a point in a drawing.
Have a form pop up and offer me three options.
Rise, Run, and Number of Risers.
Then click on the "Magic"
CommandButton.
And Poof, it will have created a polyline, that looks like stair.

I figured this would be a simple project to get me started, it had all the great elements. I have read a number of different tutorials, seen many different examples, and have learned a lot about how other projects work. So now I'm stuck. I know where to make my declarations. I know how to add a module. I know how to create a form and how to have the results of the form appear in a module. If everything is made public, why are theses values not passing thru from one module to another?

Dim Abstairs As Double     all declarations are made at top of page in thisdrawing
Dim Bbstairs As Double
Dim Cbstairs As Double
Dim R As Integer
Dim X As Double
Dim Y As Double
Dim ptstair As Variant
Dim p As Collection
Dim i As Integer

Public Sub stair()
ptstair = ThisDrawing.Utility.GetPoint(, "Select a point")
MsgBox ptstair(0) & ";" & ptstair(1) & ";" & ptstair(2)
Abstairs = ptstair(0)
Bbstairs = ptstair(1)
Cbstairs = ptstair(2)
MsgBox Abstairs & Bbstairs & Cbstairs
StrOpt.Show
='this is the name of my form.End Sub

after options in form are entered and the magic button is pushed... it should begin this module
Public Sub makestair()
MsgBox Abstairs & ";" & Bbstairs & ";" & Cbstairs       everytime this message box appears, the values are blank.
R = StrOpt.textboxR
X = StrOpt.textboxX
Y = StrOpt.textboxY
' You always have to do this with a collection before you can use it.
    Set p = New Collection
' We need r*2+1 points because we go up first; this gives us r horizontal lines
    For i = 1 To R * 2 + 1
'   This line tells us if it's odd or even; i Mod 2 = 0 means i is even
'    (i divided by 2 gives us a remainder of 0)
        If i Mod 2 = 0 Then
            p.Add "(" & Abstairs + X * (i - 2) / 2 & "," & Bbstairs + Y * i / 2 & "," & Cbstairs & ")"
        Else
            p.Add "(" & Abstairs + X * (i - 1) / 2 & "," & Bbstairs + Y * (i - 1) / 2 & "," & Cbstairs & ")"
        End If
' This just outputs the variables to the spreadsheet so that you can check it; you probably
'  won't be able to do this in AutoCAD
' This is the end of the loop; it tells i to increment by 1
    Next i
MsgBox p(1) & p(2)     this message box shows the values for p but it does not reference the point selected earlier.
I have tried different syntax for getting the addpolyline to look up the values of p from the collection and use them for the points that should be included in the polyline. I was thinking that only an array can be used in this manor for a polyline. But not sure.

End Sub

If there is a better way to do this I'm open to new directions. This is the one that made the most sense to me. I have ran across so many roadblocks, that is becoming apparant that I have so much more home work to do. And then maybe one day I will be come familiar enough to move on to acad.net. That would be nice.

Thanks all.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Do polylines only work with arrays?
« Reply #1 on: October 07, 2008, 02:55:10 PM »
First... Welcome to the swamp.

Second... it works for me.   :?
The only thing I would change is DIM to PUBLIC since you want to make these variables available to other modules.

Existing code...
Code: [Select]
Dim Abstairs As Double
Dim Bbstairs As Double
Dim Cbstairs As Double
Dim R As Integer
Dim X As Double
Dim Y As Double
Dim ptstair As Variant
Dim p As Collection
Dim i As Integer

Altered code...

Code: [Select]
Public Abstairs As Double
Public Bbstairs As Double
Public Cbstairs As Double
Public R As Integer
Public X As Double
Public Y As Double
Public ptstair As Variant
Public p As Collection
Public i As Integer

Oh, and I ALWAYS use Option Explicit which means that all variables must be declared.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

rogue

  • Guest
Re: Do polylines only work with arrays?
« Reply #2 on: October 07, 2008, 04:10:05 PM »
One thing I *like* to do, is move my Public variables to a CODE module (ThisDrawing is technically a CLASS module)

BTW, thats some pretty code for a 'new guy' ..... welcome!

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Do polylines only work with arrays?
« Reply #3 on: October 07, 2008, 04:12:49 PM »
One thing I *like* to do, is move my Public variables to a CODE module (ThisDrawing is technically a CLASS module)

BTW, thats some pretty code for a 'new guy' ..... welcome!

Same here... come to think of it.... I can't remember the last time I had ANY code in ThisDrawing (other than an old ACAD.dvb file which has long died off).
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Cathy

  • Guest
Re: Do polylines only work with arrays?
« Reply #4 on: October 07, 2008, 05:42:49 PM »

I have tried different syntax for getting the addpolyline to look up the values of p from the collection and use them for the points that should be included in the polyline. I was thinking that only an array can be used in this manor for a polyline. But not sure.


I think you are correct.  The AddPolyline method needs an array.   The VBA help is not too bad and should have examples.  The array needs to be declared as double (real numbers).  It looks like your collection is a collection of strings (text strings with numbers in them that happen to look remarkably lispy  :-) ).   You've made a really good start, getting most of it to work great! 

Co.Mnstr

  • Guest
Re: Do polylines only work with arrays?
« Reply #5 on: October 08, 2008, 10:20:49 AM »
Thank you everyone for the good 411. I'll keep you up to date on the final code.

Co.Mnstr

  • Guest
Re: Do polylines only work with arrays?
« Reply #6 on: October 08, 2008, 04:06:34 PM »
This is going well until this point. Can someone please point out where my logic is wrong, or possible where my syntax has gone bad.
Code: [Select]
Option Explicit
Dim pt As Variant
Public Ab0 As Double
Public Bb0 As Double
Public Cb0 As Double
Public abc As Double
Public R As Integer
Public X As Double
Public Y As Double
Dim ptstair As Variant
Dim p As Variant
Dim i As Integer
Dim RR
Dim PLP(0 To 2) As Double
Dim plpoints
Dim plp00 As Double
Dim plp01 As Double
Dim plp02 As Double

Public Sub SelPoint()

      pt = ThisDrawing.Utility.GetPoint(, "Select Point:")
MsgBox pt(0) & ";" & pt(1) & ";" & pt(2)
             Ab0 = pt(0)
             Bb0 = pt(1)
             Cb0 = pt(2)
StrOpt.Show                 'this calls the form StrOpt. Where I input the values for the rise, run, and number of risers.
End Sub


Public Sub makestair()

R = StrOpt.textboxR        'This is the number of risers
X = StrOpt.textboxX        'This is the Run of the riser
Y = StrOpt.textboxY        'This is the Rise of the riser
RR = (R * 2) + 1              ' This is the number of times the step needs to repeat. 4 Risers will require
                                    '9 points in the polyline
                                        ' You always have to do this with a collection before you can use it.
    Set p = New Collection
   
                                          ' i is equal to integers 1 thru RR,(number of points needed for polyline)
    For i = 1 To RR Step 1
        If i Mod 2 = 0 Then         'if i divided by 2 has a remainder of 0, an even number, then I would
                                           'like for the following formula.
            plp00 = Ab0 + X * ((i - 2) / 2)
            plp01 = Bb0 + Y * (i / 2)
            plp02 = Cb0
            MsgBox plp00 & plp01 & plp02
           
            PLP(0) = plp00: PLP(1) = plp01: PLP(2) = plp02     ' here is where I would like to take the three values and
                                                                                'and turn them back into a variant that will be used in the
                                                                                ' add polyline command.
            p.Add (PLP())
           
        Else                    'if i divided by 2 has  a remainder not equal to 0, odd number, then I
                                'would like the following formula.
            plp00 = Ab0 + X * ((i - 1) / 2)
            plp01 = Bb0 + Y * ((i - 1) / 2)
            plp02 = Cb0
            MsgBox plp00 & plp01 & plp02
           
            PLP(0) = plp00: PLP(1) = plp01: PLP(2) = plp02
            p.Add (PLP())
        End If
    Next i
                                        ' now I should have a collection of items that are variants. 
plpoints = p.Item(PLP(4))             

MsgBox plpoints

End Sub
I know I can get a point and extract the three variants. How do I do the opposite. Use three numbers and make them variants of a single variable?

Thanks, all

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Do polylines only work with arrays?
« Reply #7 on: October 08, 2008, 05:29:11 PM »
I don't have much time here but you know at run time how many points there are.
Dim pts()
dim totalpts as integer
totalpts=x
redim pts(totalpts-1)

now in your loop
pts(i*3)=  :pts(i*3 +1)= etc

I would only use lwplines so the math is different than the heavy ones

Cathy

  • Guest
Re: Do polylines only work with arrays?
« Reply #8 on: October 08, 2008, 06:08:47 PM »
Bryco's suggestion looks good.  I would get rid of the collection and just use a redim array of points.  It's confusing because the AddPolyline method doesn't actually use points -- it uses an array that contains xyzxyzxyzxyz values.  For a heavy polyline the pts should be

redim pts(RR*3-1) as double 
Where RR is the number of vertices in the polyline. -1 is because for some ridiculous reason VB starts counting at 0.  Your array should eventually contain (for 9 vertices)
pts(0) = x1
pts(1) = y1
pts(2) = z1
pts(3) = x2
pts(4) = y2
pts(5) = z2
...
pts(24) = x9
pts(25) = y9
pts(26) = z9

So in your loop i = 1 to RR, where you have
PLP(0) = plp00: PLP(1) = plp01: PLP(2) = plp02
you could change (or add)
pts((i-1)*3) = plp00: pts((i-1)*3+1) = plp01: pts((i-1)*3+2) = plp02

And since your z is always the same, you might consider using lightweight polylines -- they use less space.