Jump to content

AutoLisp Start Request


skipsophrenic

Recommended Posts

Hi all,

 

I'm thinking of trying to write a program that will take various points

on the dwg and automatically draw track and sleepers along those

points. (maybe use a polyline to define the path instead of

points/nodes)

 

Could somebody help me out here by writing the first few lines and

noting what each line does, so as i can attampt to carry it on?

 

This is NOT urgent, a little project for my own enjoyment/torture. :)

Link to comment
Share on other sites

Do you mean something like a mono rail track?

 

If so, I'd probably use a spline as the path and measure the sleepers with a block. -David

3d.gif

Link to comment
Share on other sites

Not a monorail no, train track (Railroad)

 

Only reason I'm even after to do this in lisp is because no matter how many times i go through various tutorials i cant seen to wrap my head arounf it.

Link to comment
Share on other sites

I'd probably still approach it the same way.

 

You could do all of the calculations once the path was found in autolisp but it's already built into acad.

 

find the center line, offset the tracks, measure the center line with the sleepers, extrude the tracks if needed.

 

The one thing that doesn't work is dividing or measure the tracks as true wheel points during travel. The arc lengths vary so the outer wheels always travel further. -David

Link to comment
Share on other sites

I get what your saying mate, iit's just i chose to do this because as i understand it, if I was to try writing a lisp for it, i would have to use quite a few of th programming functions in it, thus learning lisp properly.

 

(instead of looking like the lisp newb that i am! lol :lol:)

Link to comment
Share on other sites

Ok, maybe i'm trying to "run before I walk" here, but I've gone over tutorials,tried taking lisps apart and trying to figure them out, but it all confuses the hell outta me.

 

So what I'm asking i guess, is how about someone suggests something basic for me to try, i'll then post how far I get, and you tell me here I gone wrong, (And why) and see how it goes?

Link to comment
Share on other sites

Skips,

 

The way I would approach it if you are just starting is to try some basic operations, for example, changing the layer of an object - there is much to learn from an example like this, and many ways to approach it.

 

I would start with learning AutoLISP, and when you are completely competent with the AutoLISP functions, move into a bit of Visual LISP, where the structure of the AutoCAD program comes into play a bit more.

 

To get you started, I would recommend beginning with changing the DXF group codes of an object.

 

Text is a good starting point, as you can mess with height, content, layer, color, style etc and all are pretty intuitive.

 

So, to get you started, shall we go step by step through a simple program to change the text height of a selected text item? And then, if you are stuck at all, we are here to help :)

 

Lee

Link to comment
Share on other sites

Skips,

 

This should give you the general idea of how to modify an enitity using its DXF data.

 

I have tried to explain each step of the code as clearly as possible, but if you are stuck at all, just ask.

 

[i][color=#990099];; Example to Change Text height of a selected item  ;;[/color][/i]
[i][color=#990099];; to 2.0                                            ;;[/color][/i]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:ChangeHeight

      [i][color=#990099];; Here we are defining the function, we[/color][/i]
      [i][color=#990099];; use (defun c:..) to indicate that the[/color][/i]
      [i][color=#990099];; user can invoke the function from the[/color][/i]
      [i][color=#990099];; command-line.[/color][/i]

      [b][color=RED]([/color][/b][b][color=BLUE]/[/color][/b] ent[b][color=RED])[/color][/b]

      [i][color=#990099];; Inside these bracket we list all the[/color][/i]
      [i][color=#990099];; arguments needed for the function to[/color][/i]
      [i][color=#990099];; work (before the forward slash), in this[/color][/i]
      [i][color=#990099];; case none, and all the local variables[/color][/i]
      [i][color=#990099];; i.e. symbols which have a value bound to[/color][/i]
      [i][color=#990099];; them, which are used in the function.[/color][/i]

      [i][color=#990099];; These variables will be set to nil when[/color][/i]
      [i][color=#990099];; when the function is invoked and when the[/color][/i]
      [i][color=#990099];; function completes.[/color][/i]

 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b]  [i][color=#990099];; We must allow for the user not selecting[/color][/i]
      [i][color=#990099];; anything.[/color][/i]

   [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [i][color=#990099];; Both of the following conditions must[/color][/i]
        [i][color=#990099];; return True for the IF statement to proceed.[/color][/i]

     [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] ent [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#a52a2a]"\nSelect Text Object: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

     [i][color=#990099];; If the user picks something, entsel will return a[/color][/i]
     [i][color=#990099];; list with two elements. The first element will be[/color][/i]
     [i][color=#990099];; the entity name of the entity that was picked, and[/color][/i]
     [i][color=#990099];; the second element will be the actual point that was[/color][/i]
     [i][color=#990099];; picked, i.e. (3.4 5.2 0.0). We are only interested in[/color][/i]
     [i][color=#990099];; the entity name, so we use 'car' to get the first element[/color][/i]
     [i][color=#990099];; of the list, and then use 'setq' to bound this entity[/color][/i]
     [i][color=#990099];; name to a variable, so that we can reference it later.[/color][/i]

     [i][color=#990099];; Note that, if the user doesn't pick anything, entsel[/color][/i]
     [i][color=#990099];; will return nil, and so (car (entsel)) will also return[/color][/i]
     [i][color=#990099];; nil, and our IF statement will not be fullfilled.[/color][/i]

     [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"TEXT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] ent[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

     [i][color=#990099];; This is the second of our conditions that must be[/color][/i]
     [i][color=#990099];; met for the AND statement to return True, and hence the[/color][/i]
     [i][color=#990099];; IF statement to proceed.[/color][/i]

     [i][color=#990099];; The 'entget' function will return the DXF data associated[/color][/i]
     [i][color=#990099];; with an entity name, this includes all the data that[/color][/i]
     [i][color=#990099];; describe that particular entity.[/color][/i]

     [i][color=#990099];; The Group code '0' is the entity type, and each group[/color][/i]
     [i][color=#990099];; code is listed as the code coupled with the data associated[/color][/i]
     [i][color=#990099];; with that code, in this way:[/color][/i]
     [i][color=#990099];;         ((0 . "TEXT") (40 . 2.5) ...)[/color][/i]
     [i][color=#990099];; We can use the 'assoc' function to look at the first element[/color][/i]
     [i][color=#990099];; in each of these lists, and retrieve the one that starts with '0'.[/color][/i]

     [i][color=#990099];; We now have (0 . "TEXT"), so we can get at the second part of this[/color][/i]
     [i][color=#990099];; using the 'cdr' function. We can then test to see whether this equals[/color][/i]
     [i][color=#990099];; "TEXT" using the 'eq' function. If not, the user has selected an[/color][/i]
     [i][color=#990099];; object that isn't text.[/color][/i]

   [b][color=RED])[/color][/b] [i][color=#990099]; End AND[/color][/i]

   [i][color=#990099];; Now we can proceed with the THEN statement, this is the statement[/color][/i]
   [i][color=#990099];; that is evaluated if the text expression for the IF function is[/color][/i]
   [i][color=#990099];; satisfied.[/color][/i]

   [b][color=RED]([/color][/b][b][color=BLUE]entmod[/color][/b] [i][color=#990099];; Modify the entity after doing the following...[/color][/i]

     [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b]  [i][color=#990099];; Substitute[/color][/i]

       [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]40[/color][/b] [b][color=#009999]2.0[/color][/b][b][color=RED])[/color][/b]  [i][color=#990099];; A dotted pair '(40 . 2.0)[/color][/i]

         [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]40[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] ent[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [i][color=#990099];; For the DXF group 40 entry in the list[/color][/i]

       [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] ent[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [i][color=#990099];; The data list that is being operated on.[/color][/i]

   [i][color=#990099];; This is the business end of the function. Here we are using the[/color][/i]
   [i][color=#990099];; 'subst' function to substitute '(40 . 2.0) for the entry that[/color][/i]
   [i][color=#990099];; starts with 40 in the DXF data list.[/color][/i]

   [i][color=#990099];; The DXF code 40 represents the Text Height of the text, and,[/color][/i]
   [i][color=#990099];; once we have made the substitution, we must update the entity[/color][/i]
   [i][color=#990099];; using the 'entmod' function.[/color][/i]

   [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#a52a2a]"\n** Object is Not Text **"[/color][/b][b][color=RED])[/color][/b]

   [i][color=#990099];; This is the ELSE statement for the IF function, so here we[/color][/i]
   [i][color=#990099];; are alerting the user that they have not picked a text object.[/color][/i]

 [b][color=RED])[/color][/b] [i][color=#990099]; End IF[/color][/i]

 [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b] [i][color=#990099];; Exit Cleanly.[/color][/i]

[b][color=RED])[/color][/b] [i][color=#990099];; End DEFUN[/color][/i]

     

Link to comment
Share on other sites

Cheers Lee,

 

I'll have a look at that tomorrow (When had some sleep! LOL) if i do get any probs understanding i'll let you know.

 

(If i do, then i'll feel silly!!)

Link to comment
Share on other sites

Here's 1 I thought would be fun. It's all in inches ( I'm just not a metric guy )

 

(defun c:gate (/ hgt len qty sp xp maxx bn)

 (initget 7)
 (setq hgt (getdist "\nHeight To Pivot Point:   "))

 (while (or (not len)
            (< len 96))
        (initget 7)
        (setq len (getdist "\nGate Length:   ")))

 (initget 6)
 (setq qty (getint "\nNumber Of Lights <4>:   "))
 (if (not qty)
     (setq qty 4))


;;;START THE BLOCK DEFINITION
(entmake (list (cons 0 "BLOCK")(list 10 0 0 0)(cons 70 0)
              (cons 2 (strcat "GATE" (rtos len 2 0) "x" (itoa qty)))))

;;;MAKE THE COUNTER WEIGHT AND PIVOT
(entmake (list (cons 0 "CIRCLE")(cons 8 "3D")(cons 39 10)(cons 10 (list 8 0 -5))(cons 40 1)(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "SOLID")(cons 8 "3D")(cons 39 4)(cons 10 (list -2 -5 -2))(cons 11 (list 0 -5 -2))(cons 12 (list -2 5 -2))(cons 13 (list 0 5 -2))))
(entmake (list (cons 0 "SOLID")(cons 8 "3D")(cons 39 4)(cons 10 (list -32.48528137 -5 6.48528137))(cons 11 (list -30.48528137 -5 6.48528137))(cons 12 (list -32.48528137 5 6.48528137))(cons 13 (list -30.48528137 5 6.48528137))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 0 2 5))(cons 11 (list 0 -2 5))(cons 12 (list 11.17157288 2 5))(cons 13 (list 12.82842712 -2 5))(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 11.17157288 2 5))(cons 11 (list 12.82842712 -2 5))(cons 12 (list 19.65685425 10.48528137 5))(cons 13 (list 21.3137085 6.48528137 5))(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 19.65685425 10.48528137 5))(cons 11 (list 21.3137085 6.48528137 5))(cons 12 (list 32.48528137 10.48528137 5))(cons 13 (list 32.48528137 6.48528137 5))(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 19.65685425 10.48528137 -7))(cons 11 (list 21.3137085 6.48528137 -7))(cons 12 (list 32.48528137 10.48528137 -7))(cons 13 (list 32.48528137 6.48528137 -7))(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 11.17157288 2 -7))(cons 11 (list 12.82842712 -2 -7))(cons 12 (list 19.65685425 10.48528137 -7))(cons 13 (list 21.3137085 6.48528137 -7))(cons 210 (list 0 1 0))))
(entmake (list (cons 0 "TRACE")(cons 8 "3D")(cons 39 2)(cons 10 (list 0 2 -7))(cons 11 (list 0 -2 -7))(cons 12 (list 11.17157288 2 -7))(cons 13 (list 12.82842712 -2 -7))(cons 210 (list 0 1 0))))


;;;MAKE THE LIGHTS
(setq sp (/ len qty)
     xp (* sp 0.5))

(repeat qty
 (entmake (list (cons 0 "CIRCLE")(cons 8 "3D-RED")(cons 39 2)(cons 10 (list xp 6 -1))(cons 40 3)(cons 210 (list 0 -1 0))))
 (entmake (list (cons 0 "CIRCLE")(cons 8 "3D-BLACK")(cons 39 1.5)(cons 10 (list xp 0 2))(cons 40 0.5)))
 (setq xp (+ xp sp)))

;;;THE ARM STARTER SECTION
(entmake (list (cons 0 "SOLID")(cons 8 "3D-WHITE")(cons 39 2)
              (cons 10 (list 0 -2 -1))
              (cons 11 (list 0  2 -1))
              (cons 12 (list 2 -2 -1))
              (cons 13 (list 6  2 -1))
              (cons 210 (list 0 -1 0))))

;;;THE RED AND WHITE STRIPS
(setq xp 2
   maxx (- len 2))

(while (< xp len)
  (entmake (list (cons 0 "SOLID")(cons 8 "3D-RED")(cons 39 2)
                 (cons 10 (list (min maxx (+ xp 0))  -2 -1))
                 (cons 11 (list (min maxx (+ xp 4))   2 -1))
                 (cons 12 (list (min maxx (+ xp 24)) -2 -1))
                 (cons 13 (list (min maxx (+ xp 26))  2 -1))
                 (cons 210 (list 0 -1 0))))
  (entmake (list (cons 0 "SOLID")(cons 8 "3D-WHITE")(cons 39 2)
                 (cons 10 (list (min maxx (+ xp 24)) -2 -1))
                 (cons 11 (list (min maxx (+ xp 26))  2 -1))
                 (cons 12 (list (min maxx (+ xp 48)) -2 -1))
                 (cons 13 (list (min maxx (+ xp 52)) 2 -1))
                 (cons 210 (list 0 -1 0))))
  (setq xp (+ xp 48)))


;;;THE END CAP
(entmake (list (cons 0 "SOLID")(cons 8 "3D-WHITE")(cons 39 2)
              (cons 10 (list (- len 2)  2 -1))
              (cons 11 (list (- len 0)  2 -1))
              (cons 12 (list (- len 6) -2 -1))
              (cons 13 (list (- len 0) -2 -1))
              (cons 210 (list 0 -1 0))))

;;;FINISH THE BLOCK DEFITION
(setq bn (entmake (list (cons 0 "ENDBLK")(cons 8 "0"))))

;;;SET THE LAYER COLORS
(command "_.LAYER" "_C" 254 "3D" "_C" 1 "3D-RED" "_C" 7 "3D-WHITE" "")

;;;INSERT THE NEW BLOCK AT THE GIVEN HEIGHT
(entmake (list (cons 0 "INSERT")(cons 2 bn)(cons 8 "0")
              (cons 10 (list 0 0 hgt))))

(prin1))

 

Have Fun! -David

ar-gate.jpg

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