Author Topic: AutoLISP routine help!!!! (by collegeCAD)  (Read 41426 times)

0 Members and 2 Guests are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
AutoLISP routine help!!!! (by collegeCAD)
« Reply #30 on: October 01, 2003, 02:02:18 PM »
lol.

Just for an example. Here is a function that i use everyday. It is in a file that is loaded upon startup cause i use it so much.
Code: [Select]
;Makes a new layer and asks for the color.
(defun C:MLL ()
   (setq name (strcase (getstring "\nEnter the new layer name: ")))
   (setq col (acad_colordlg 1 T))
    (command "Layer" "m" name "c" col name "")
  (princ)
)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

t-bear

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #31 on: October 01, 2003, 02:35:30 PM »
ok....now this interests me.  Here's the scenario...I have 30 parts that need to be dim'd on a cut sheet, each in its own vport.  The assy is xref'd into MS and I freeze all but part 01 in the first PS vport. After diming the part I want to be able to copy it to the right and freeze 01 and thaw 02.  so far, so good.  Here's the problem...for 30 parts, I need 30 dim layers...named "DIM01...DIM02...etc with color "blue".  I see the possibilities in CC's lisp but don't know how to tweak it to suit my needs.  I don't need the linetype section...all continuous. Somehow I need to have the routine create X number of layers, numbered sequentially, through a user input ("how many layers do you need?)...(30).  Does this make sense?

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #32 on: October 01, 2003, 03:23:11 PM »
t-bear, hold on 30 seconds :)

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #33 on: October 01, 2003, 03:23:57 PM »
Will this work for you, Bear:
Code: [Select]
;;; "User Friendly Layer Setup"
;;; layersetup1.lsp
;;; By: Jon Nelson
;;; DFTG1352
;;; Thomas Longnecker

;;; Altered and commented by someone else
;;; Ripped apart for altered use by another

(defun c:layersetup ()
     (vl-load-com)
     (setq num 1
  name "DIM"
  reset (getvar 'clayer)
  cnt 1)
     (initget 7) ;I initially put 6. Edited to make it better.
     (setq rept (getint "\nHow many layers do you want?: "))
     (repeat rept
 (if (> cnt 9)
     (setq layName (strcat name (rtos cnt 2 0)))
     (setq layName (strcat name "0" (rtos cnt 2 0)))
 )
 (setq cnt (1+ cnt))
 (if (snvalid layName)
      (setq n (append (list layName) n))
      (setq tn nil)
 )
     )
     (foreach item n
     (command "Layer" "m" item "c" 5 "" "")
 )
     (setvar 'clayer reset)
     (princ)
)

(prompt "Enter LAYERSETUP to start")

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #34 on: October 01, 2003, 03:59:23 PM »
Darn, beaten by Daron again :)  Nice work

I was just sitting with something completely different and yet similar. Watch out for large numbers. It doesn't care if the user happened to type 500000 .. it'll make 500000 layers then (I think? Didn't test that particular case).

Code: [Select]
(defun C:ADDLAYERS ()
  ;; put whatever color number here
  (makeLayers 5)
)

(defun makeLayers (cnum / cnt ctrl layname laynum laypref zero zeroln)
  (setq ctrl 0)
  (initget 6)
  (cond ((and (numberp cnum) (setq laynum (getint "\nNumber of layers: ")))
         (initget 128 "Dim")
         (setq laypref (cond ((setq laypref (getkword "\nLayer prefix <Dim>: "))
                              (setq laypref (strcase laypref)))
                             ("DIM")))
         (setq cnt    1
               zeroln (strlen (strcat laypref (itoa laynum)))
               zero   ""
         )
         (repeat laynum
           (repeat (- zeroln (strlen (strcat laypref (itoa cnt))))
             (setq zero (strcat zero "0"))
           )
           (setq layname (strcat laypref zero (itoa cnt))
                 cnt     (1+ cnt)
                 zero    ""
           )
           (if (entmake (list '(0 . "LAYER")
                              '(100 . "AcDbSymbolTableRecord")
                              '(100 . "AcDbLayerTableRecord")
                              (cons 2 layname)
                              (cons 62 cnum)
                              '(70 . 0)))
             (setq ctrl (1+ ctrl))
           )
         )
        )
  )
  (princ (strcat "\n" (itoa ctrl) " layers created"))
  (princ)
)

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #35 on: October 01, 2003, 04:03:31 PM »
By the way, this is a good chance to notice the difference between the command interface and the direct access to entity creation/modification. Try creating 200 layers with each of these functions and check the time it takes!

daron

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #36 on: October 01, 2003, 04:14:02 PM »
I'm glad you did yours different, Stig. I was just taking what you left CC with and modifying it to suit Bear's needs. I was thinking to use vla subs, but chose not to alter the original commands style. I think in the case of vla-properties and your entity creation/mod, your's is still the better way to go. So, I think with that, I just learned something new. Just to make sure I got this right, when adding objects to a collection, it's better to access the collection. Hm?

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
AutoLISP routine help!!!! (by collegeCAD)
« Reply #37 on: October 01, 2003, 08:34:28 PM »
My " two cents worth"  down-n-dirty ActiveX method.
Stig... got your stop watch handy? I'd like a race. <G>

Code: [Select]
(defun c:LayerCreator ( / lo num ll)

  (vl-load-com)

  (setq lo
        (vla-get-layers
          (vla-get-activedocument
            (vlax-get-acad-object)
            )
          )
        )

  (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))

  (if
    (and num (< num 200))
    (while
      (> num 0)
      (cond
        ((< num 10)
         (setq ll (cons (strcat "DIM0"(itoa num)) ll)))
        (T
          (setq ll (cons (strcat "DIM"(itoa num)) ll))
          )
        )
      (setq num (1- num))
      ); while
    ; else
    (prompt (strcat "\nAre you kidding..."(itoa num)" layers...."))
    ); if

  (if ll
    (mapcar
      '(lambda (x / newlayer)
         (setq newlayer (vla-add lo x))
         (vlax-put-property newlayer  'Color 5)
         (vlax-release-object newlayer)
         )
      ll)
    ); if

  (if (not (vlax-object-released-p lo))
    (vlax-release-object lo)); if

  (princ)
  ); defun
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
AutoLISP routine help!!!! (by collegeCAD)
« Reply #38 on: October 01, 2003, 08:53:21 PM »
Quote from: Mark Thomas
...
Stig... got your stop watch handy? I'd like a race. <G>


*Se7en sits back, hunkers down with some popcorn and gets ready for the show.

Fight, Fight! ...Kick his ass, kick - his - ass!

w00t!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
AutoLISP routine help!!!! (by collegeCAD)
« Reply #39 on: October 01, 2003, 08:56:37 PM »
Quote from: Mark Thomas

Code: [Select]
... (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))


Dude?! Dahell is that?! Stigs can do 50000 layers!? Yours can only do 199 ?!

...Wuss!

Edit: It appears that i missed a zero in Stig's functions capibilities.  I added it.

...Gee Mark, are you sure your not gonna trip on your dress when you run that progy?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #40 on: October 02, 2003, 06:46:21 AM »
And the result is ... !!!!

Two tests were done. One for each command in a fresh drawing each (called 15 times) and one in the same drawing where the commands were called in this sequence (called 10 times each):

AddLayer, LayerSetup, LayerCreator
LayerSetup, LayerCreator, AddLayer
LayerCreator, AddLayer, LayerSetup

Each command set up 100 layers and was followed by garbage collection, purge and garbage collection. Timer was started as soon as user inputs were done. The code below shows were timers were started and ended in each routine along with the timer and cleanup code used.

First, the results!! Unit is in seconds.

Code: [Select]
New drawings, only one command used in each drawing
LayerSetup   AddLayers    LayerCreator
 0,515989     0,046992     0,094025
 0,968975     0,061999     0,093985
 1,547004     0,046992     0,092979
 2,124994     0,046992     0,092979
 2,735008     0,061999     0,078978
 3,391009     0,063005     0,094025
 4,157008     0,063005     0,094025
 5,640001     0,063005     0,092978
 6,531005     0,047033     0,109997
 7,344036     0,061999     0,092978
 8,296998     0,063005     0,094025
 9,218015     0,061999     0,109032
10,436998     0,063005     0,108992
11,264996     0,063005     0,093019
12,202991     0,063005     0,108992

Averages:
 5,758335     0,058469    0,096734

Same drawing, called in sequence as mentioned
LayerSetup   AddLayers    LayerCreator
 0,484005     0,045986     0,092979
 1,078070     0,046992     0,078012
 1,922017     0,045986     0,078012
 2,688016     0,063005     0,109997
 3,592980     0,077972     0,108992
 4,922001     0,061999     0,108992
 6,077980     0,078012     0,108992
 7,280990     0,078978     0,108992
 9,188001     0,078012     0,108992
10,671034     0,078012     0,108992

Averages:
 4,790509     0,065495     0,101295


Notice anything in particular with the command pipeline in each test?

Here's the code:
Code: [Select]
(defun startTimer ()
  (setq time (getvar "DATE"))
)
(defun endTimer (func)
  (setq time    (- (getvar "DATE") time)
        seconds (* 86400.0 (- time (fix time)))
  )
  (gc)
  (outPut seconds func)
)
(defun outPut (secs def)
  (princ "\nPurging...")
  (command "PURGE" "Layers" "*" "N")
  (gc)
  (princ (strcat "\nTimed " def ": " (rtos secs 2 6)))
  (princ)
)

(defun C:ADDLAYERS ()
  ;; put whatever color number here
  (makeLayers 5)
)
(defun makeLayers (cnum / cnt ctrl layname laynum laypref zero zeroln)
  (setq ctrl 0)
  (initget 6)
  (cond ((and (numberp cnum) (setq laynum (getint "\nNumber of layers: ")))
         (initget 128 "Dim")
         (setq laypref (cond ((setq laypref (getkword "\nLayer prefix <Dim>: "))
                              (setq laypref (strcase laypref)))
                             ("DIM")))
         (startTimer)
         (setq cnt    1
               zeroln (strlen (strcat laypref (itoa laynum)))
               zero   "")
         (repeat laynum
           (repeat (- zeroln (strlen (strcat laypref (itoa cnt))))
             (setq zero (strcat zero "0")))
           (setq layname (strcat laypref zero (itoa cnt))
                 cnt     (1+ cnt)
                 zero    "")
           (if (entmake (list '(0 . "LAYER")
                              '(100 . "AcDbSymbolTableRecord")
                              '(100 . "AcDbLayerTableRecord")
                              (cons 2 layname)
                              (cons 62 cnum)
                              '(70 . 0)))
             (setq ctrl (1+ ctrl))))))
  (princ (strcat "\n" (itoa ctrl) " layers created"))
  (princ)
  (endTimer (vl-symbol-name 'makeLayers))
)


(defun c:layersetup ()
  (vl-load-com)
  (setq num   1 name  "DIM" reset (getvar 'clayer) cnt   1)
  (initget 7)       ;I initially put 6. Edited to make it better.
  (setq rept (getint "\nHow many layers do you want?: "))
  (startTimer)
  (repeat rept
    (if (> cnt 9)
      (setq layName (strcat name (rtos cnt 2 0)))
      (setq layName (strcat name "0" (rtos cnt 2 0))))
    (setq cnt (1+ cnt))
    (if (snvalid layName)
      (setq n (append (list layName) n))
      (setq tn nil)))
  (foreach item n
    (command "Layer" "m" item "c" 5 "" ""))
  (setvar 'clayer reset)
  (princ)
  (endTimer (vl-symbol-name 'c:layersetup))
)


(defun c:LayerCreator (/ lo num ll)
  (vl-load-com)
  (setq lo (vla-get-layers (vla-get-activedocument
             (vlax-get-acad-object))))
  (initget 7)
  (setq num (getint "\nHow many dim layers to create? (must be < 200) : "))
  (startTimer)
  (if (and num (< num 200))
     (while (> num 0)
        (cond ((< num 10)
               (setq ll (cons (strcat "DIM0" (itoa num)) ll)))
              (T (setq ll (cons (strcat "DIM" (itoa num)) ll))))
        (setq num (1- num)))              ; while
     (prompt (strcat "\nAre you kidding..." (itoa num) " layers....")))
  (if ll (mapcar
      '(lambda (x / newlayer)
         (setq newlayer (vla-add lo x))
         (vlax-put-property newlayer 'Color 5)
         (vlax-release-object newlayer)) ll))
  (if (not (vlax-object-released-p lo))
    (vlax-release-object lo))
  (princ)
  (endTimer (vl-symbol-name 'c:LayerCreator))
)
[/code]

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
AutoLISP routine help!!!! (by collegeCAD)
« Reply #41 on: October 02, 2003, 08:38:48 AM »
Very interesting Stig, I am truly enjoying this. If you don't mind
I'm going to make a new thread and add the tested code to it.


I also tried creating 1000 layers with each.

1000 LAYERS
Timed MAKELAYERS: 0.172037      Stig's
Timed C:LAYERCREATOR: 0.297001  Mark's
Timed C:LAYERSETUP: 4.264994    Daron's

I'm going back to the pits for a tune up. <g>
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
AutoLISP routine help!!!! (by collegeCAD)
« Reply #42 on: October 02, 2003, 08:46:20 AM »
:)
Will be interesting to see if you can tune activex to match lisp in performance.

Just for the fun of it, I tried creating 12450 layers. AddLayers took 16 seconds, LayerSet took 10 minutes - not to create the layers, though, but to fold with a fatal error! Never got around to test LayerCreator after that  :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
AutoLISP routine help!!!! (by collegeCAD)
« Reply #43 on: October 02, 2003, 08:47:23 AM »
:?  huh!? That is cool!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
AutoLISP routine help!!!! (by collegeCAD)
« Reply #44 on: October 02, 2003, 08:53:08 AM »
12450 layers.......

C:LAYERCREATOR: 15.125003
TheSwamp.org  (serving the CAD community since 2003)