Author Topic: Simple 2x4 ... Not for me ...  (Read 2828 times)

0 Members and 1 Guest are viewing this topic.

Hangman

  • Guest
Simple 2x4 ... Not for me ...
« on: September 30, 2005, 03:19:41 PM »
Could someone help me with this please.  I'm having a bit of a problem keeping the existing input.  This is a simple lisp with a bit of a twist.  What I want to do is keep the first input to use over and over.  So if you input 24 for 2 x 4, when the program is finished, hit return to run the program again, leaving 24 as the new default, it should create a 2 x 4.  Well, it does, but the command line shows something different.  Also, when you input 24B for 2 x 4 Block, when repeating the command and entering 24, it still creates the block.
Confusing enough for you ???
Here's the entire code.  Try it, see how it works for you and if you have any ideas to help my learning, please, I'm open to suggestions.
Thanks.

Code: [Select]
;;Written By Pasta - ARW Engineers     Sept 2005
;;
;;This program automatically draws any size timber member or blocking
;;to the correct width and length.
;;
;;
(defun c:ttimber (/ oldecho oldos oldortho oldlayr oldpline fn
    spt pt1 pt2 pt3 set-a set-b blnm
)
  (defun $error (msg /)
    (if (or (= msg "Function cancelled")
            (/= msg "quit / exit abort")
        )
        (princ (strcat "Error: " msg))
    )
    (command ".undo" "e" "undo" "")
    (command ".redraw")
    (setvar "cmdecho" oldecho)
    (setq *error* old_err)
    (setq oldecho nil)
    (setq oldos nil)
    (princ)
  );end error
  (command ".undo" "be")
  (setq old_err *error*
        *error* $error
        oldecho (getvar "cmdecho")
        oldos (getvar "osmode")
        oldortho (getvar "orthomode")
        oldlayr (getvar "clayer")
        oldpline (getvar "plinewid")
        spt (getvar "viewctr")
        fn 0)
  (setvar "cmdecho" 1)
;;
;;--Defaults
  (setq defw1 w1)
  (if (= defw1 nil)
    (setq defw1 2))
  (setq defl1 l1)
  (if (= defl1 nil)
    (setq defl1 6))
;;
;;--Get Info
  (initget "12 12B 13 13B 14 14B 16 16B 18 18B 110 110B 112 112B
            22 22B 23 23B 24 24B 26 26B 28 28B 210 210B 212 212B
            33 33B 34 34B 36 36B 38 38B 310 310B 312 312B 314 314B 316 316B
            43 43B 44 44B 46 46B 48 48B 410 410B 412 412B 414 414B 416 416B
            64 64B 66 66B 68 68B 610 610B 612 612B 614 614B 616 616B 618 618B
            86 86B 88 88B 810 810B 812 812B 814 814B 816 816B 818 818B 820 820B 822 822B")
  (setq tsize (getstring (strcat "\nEnter size of Timber (i.e. 24 for 2 x 4) <" (rtos defw1 2 0) " x " (rtos defl1 2 0) ">: ")))
  (or (/= w1 "") (setq w1 defw1))
  (or (/= l1 "") (setq l1 defl1))
  (setq wstr (substr tsize 1 1))
  (setq w1 (atoi wstr))
  (setq lstr (substr tsize 2))
  (setq l1 (atoi lstr))
;
  (if (= w1 1)
    (setq w2 (- w1 0.25)))
  (if (= w1 2)
    (setq w2 (- w1 0.5)))
  (if (and (> w1 2) (< w1 8))
    (setq w2 (- w1 0.5)))
  (if (>= w1 8)
    (setq w2 (- w1 0.75)))
;
  (if (= l1 2)
    (setq l2 (- l1 0.5)))
  (if (and (> l1 2) (< l1 8))
    (setq l2 (- l1 0.5)))
  (if (>= l1 8)
    (setq l2 (- l1 0.75)))
;;
;;--Points
  (setq pt1 (polar spt 0 l2))
  (setq pt2 (polar pt1 (/ pi 2) w2))
  (setq pt3 (polar pt2 pi l2))
;;
;;--Layer
  (if (= (tblsearch "layer" "HATCH") nil)
    (command ".-layer" "new" "HATCH" "color" "8" "HATCH" ""))
  (command ".-layer" "thaw" "HATCH" "on" "HATCH" "")
  (if (= (tblsearch "layer" "OBJECT") nil)
    (command ".-layer" "new" "OBJECT" "color" "3" "OBJECT" ""))
  (command ".-layer" "thaw" "OBJECT" "on" "OBJECT" "")
;;
;;--Draw Timber
  (setvar "clayer" "0")
  (setvar "plinewid" 0)
  (setvar "osmode" 0)
  (command ".rectang" spt pt2)
  (setq set-a (ssget "l"))
  (setvar "clayer" "HATCH")
  (if (/= (wcmatch tsize "*B") T)
    (progn
      (command ".line" spt pt2 "")
      (setq set-b (ssget "l"))
      (command ".line" pt1 pt3 "")
      (setq set-b (ssadd (entlast) set-b))
      (command ".purge" "b" "" "n")
      (while (/= (tblsearch "block" (strcat (rtos w1 2 0) "x" (rtos l1 2 0) "Timber" (itoa fn))) nil)
        (setq fn (+ fn 1)))
      (setq blnm (strcat (rtos w1 2 0) "x" (rtos l1 2 0) "Timber" (itoa fn)))
      (command ".block" blnm spt set-a set-b "")
      (setvar "orthomode" 1)
      (setvar "clayer" "OBJECT")
      (setvar "osmode" oldos)
      (command ".insert" blnm pause "" "" pause)
    ))
  (if (= (wcmatch tsize "*B") T)
    (progn
      (command ".line" pt1 pt3 "")
      (setq set-b (ssget "l"))
      (command ".purge" "b" "" "n")
      (while (/= (tblsearch "block" (strcat (rtos w1 2 0) "x" (rtos l1 2 0) "Block" (itoa fn))) nil)
        (setq fn (+ fn 1)))
      (setq blnm (strcat (rtos w1 2 0) "x" (rtos l1 2 0) "Block" (itoa fn)))
      (command ".block" blnm spt set-a set-b "")
      (setvar "orthomode" 1)
      (setvar "clayer" "OBJECT")
      (setvar "osmode" oldos)
      (command ".insert" blnm pause "" "" pause)
    ))
;
;--Re-Set Variables
  (setvar "plinewid" oldpline)
  (setvar "orthomode" oldortho)
  (setvar "clayer" oldlayr)
  (setvar "cmdecho" oldecho)
  (command ".undo" "e")
  (princ)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Simple 2x4 ... Not for me ...
« Reply #1 on: October 01, 2005, 12:13:07 AM »
I made a few changes for you to consider but i don't understand
what you are doing with the block names.

Code: [Select]
;;Written By Pasta - ARW Engineers                  Sept 2005
;;
;;This program automatically draws any size timber member or blocking
;;to the correct width and length.
;;
;;
(defun c:ttimber (/ oldecho oldos oldortho oldlayr oldpline fn spt
                  pt1 pt2 pt3 L1 L2 OLDECHO OLDOS TSIZE W1 W2
                  set-a set-b blnm *error* bname
                 )
  (defun *error* (msg /)
    (if (or (= msg "Function cancelled")
            (/= msg "quit / exit abort")
        )
      (princ (strcat "Error: " msg))
    )
    (command ".undo" "e" "undo" "")
    (command ".redraw")
    (setvar "cmdecho" oldecho)
    (setq oldecho nil)
    (setq oldos nil)
    (princ)
  ) ;end error


  (command ".undo" "be")
  (setq oldecho  (getvar "cmdecho")
        oldos    (getvar "osmode")
        oldortho (getvar "orthomode")
        oldlayr  (getvar "clayer")
        oldpline (getvar "plinewid")
        spt      (getvar "viewctr")
        fn       0
  )
  (setvar "cmdecho" 1)
  ;;
  ;;--Defaults
  (or *tsize* (setq *tsize* "24B")) ; start with 24B

  ;;

  ;;--Get Info
  (initget
    "12 12B 13 13B 14 14B 16 16B 18 18B 110 110B 112 112B
     22 22B 23 23B 24 24B 26 26B 28 28B 210 210B 212 212B
     33 33B 34 34B 36 36B 38 38B 310 310B 312 312B 314 314B 316 316B
     43 43B 44 44B 46 46B 48 48B 410 410B 412 412B 414 414B 416 416B
     64 64B 66 66B 68 68B 610 610B 612 612B 614 614B 616 616B 618 618B
     86 86B 88 88B 810 810B 812 812B 814 814B 816 816B 818 818B 820 820B 822 822B"
  )
  (setq
    tsize (getkword (strcat "\nEnter size of Timber (i.e. 24 for 2 x 4) <"
                            (substr *tsize* 1 1)
                            " x "
                            (substr *tsize* 2)
                            ">: "
                    )
          )
  )
  (if tsize
    (setq *tsize* tsize)
    (setq tsize *tsize*)
  )

  (setq w1 (atoi (substr tsize 1 1)))
  (setq l1 (atoi (substr tsize 2)))
  ;
  (cond
    ((= w1 1)
     (setq w2 (- w1 0.25))
    )
    ((< w1 8)
     (setq w2 (- w1 0.5))
    )
    (t
     (setq w2 (- w1 0.75))
    )
  )


  ;
  (if (< l1 8)
    (setq l2 (- l1 0.5))
    (setq l2 (- l1 0.75))
  )


  ;;
  ;;--Points
  (setq pt1 (polar spt 0 l2))
  (setq pt2 (polar pt1 (/ pi 2) w2))
  (setq pt3 (polar pt2 pi l2))
  ;;
  ;;--Layer
  (if (= (tblsearch "layer" "HATCH") nil)
    (command ".-layer" "new" "HATCH" "color" "8" "HATCH" "")
    (command ".-layer" "thaw" "HATCH" "on" "HATCH" "")
  )

  (if (= (tblsearch "layer" "OBJECT") nil)
    (command ".-layer" "new" "OBJECT" "color" "3" "OBJECT" "")
    (command ".-layer" "thaw" "OBJECT" "on" "OBJECT" "")
  )

  ;;
  ;;--Draw Timber or Block
  (setvar "clayer" "0")
  (setvar "plinewid" 0)
  (setvar "osmode" 0)
  (setq bname (strcat (rtos w1 2 0) "x" (rtos l1 2 0)))

  (command ".rectang" spt pt2)
  (setq set-a (ssget "l")
        set-b (ssadd)
  )

  (setvar "clayer" "HATCH")
  (if (wcmatch tsize "*B") ; yes has B
    (command ".line" pt1 pt3 "")
    (progn ; no  B
      (command ".line" spt pt2 "")
      (setq set-b (ssadd (entlast) set-b))
      (command ".line" pt1 pt3 "")
    )
  )

  (setq set-b (ssadd (entlast) set-b))
  (command ".purge" "b" "" "n")
  (while ;  ?????????????  Why  ????????????????????
    ;;  this will create a new block ever time it is used.
    (/= (tblsearch "block"
                   (strcat bname "Block" (itoa fn))
        )
        nil
    )
     (setq fn (+ fn 1))
  )


  (if (wcmatch tsize "*B") ; yes has B
    (setq blnm (strcat bname "Block" (itoa fn)))
    (setq blnm (strcat bname "Timber" (itoa fn)))
  )

  (command ".block" blnm spt set-a set-b "")

  (setvar "orthomode" 1)
  (setvar "clayer" "OBJECT")
  (setvar "osmode" oldos)
  (command ".insert" blnm pause "" "" pause)


  ;
  ;--Re-Set Variables
  (setvar "plinewid" oldpline)
  (setvar "orthomode" oldortho)
  (setvar "clayer" oldlayr)
  (setvar "cmdecho" oldecho)
  (command ".undo" "e")
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Hangman

  • Guest
Re: Simple 2x4 ... Not for me ...
« Reply #2 on: October 03, 2005, 05:03:25 PM »
Cab,
Thanks for your help.  I do really appreciate it.  Let's see if I can explain this.
The reasoning behind the naming of the blocks in the fashion I have there is that when I bind them to send to other contractors, I run into the problem of having two or three or four details with the same named block.  When binding into the main sheet, they tend to get scaled to the sheet, all with the same name and basically, makes a general mess of things.
However, I do not necessarily want to create a new block each time it's used in the same drawing, but I do want to create a new block for 2x4, 2x6, 2x8, etc. in the same drawing.  Another words, I do not want to use the 2x4 block for a 2x6, having to stretch it or scale it to get the desired size.  And I can't use a generic block like a 2xTimber or 2xBlock naming system, basically I need individual pieces.  But if I'm using five 2 x 4's, copying them would be fine.
Now I use the Timber much more than the Blocking, that's why I started with the 2 x 4 rather than the 2 x 4B as you did with your code.
I like your code though, it's clean.  Helps me understand where I am goofing up.  However, I have noticed even with your code, you cannot do an enter repeat for a 2 x 4, it crashes with the request of re-defining it.
This should be easily remedied though, something like this I'm thinking:
(mind you this is a rough draft, I'm not that good yet)
Code: [Select]
(if (= (tblsearch "block" (strcat bname "Timber" (itoa fn)
                                 )
        )
    )
    (command ".insert" blnm pause "" "" pause)
;else
    (setq fn (+ fn 1)
    )

Mind you this is just thrown together, I'm not efficient enough without researching and testing before being confident with my code.
More comments would be wonderful, please let me know what I can to do improve my work and leave some ideas of this code.
Thanks.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Simple 2x4 ... Not for me ...
« Reply #3 on: October 03, 2005, 05:22:18 PM »
As for the error, when are you pressing the Enter key & getting the error?
Could you post the messages you get at the command line?

The blocks are not scaled correctly when you bind them? I haven't had that problem but
I don't remember scaling them at differing amounts.

Do the 2X objects have to be blocks?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter Jamtgaard

  • Guest
Re: Simple 2x4 ... Not for me ...
« Reply #4 on: October 03, 2005, 05:52:24 PM »
Have you considered a parametric timber creator?

It could create in your blocks collection any size timber on the fly without having to have a thousand predefined blocks?

Peter Jamtgaard P.E.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Simple 2x4 ... Not for me ...
« Reply #5 on: October 03, 2005, 06:19:22 PM »
Add a vote for parametrics.

Once upon a time (1995) I wrote a program to model Electrical Raceway. Based on initial communications it required a block library of a couple hundred members. No problem. Took me about 2 days to make the blocks, about 2 weeks to design and code a real nice interface. Worked fine for about 2 years. Then the users wanted a suite of options that would have necessitated a block library of about 1200 blocks. I'll write 100 pages of code before I create 1200 blocks by hand so I threw the existing project into the hex hamper and started over. In the end (about a month of programming) the program could create a little over 16000 (not a typo) unique fittings, entirely from code, i.e. parametrically defined on the fly, and I was able to incorporate a host of features they didn't think would be possible.

Gratuitous Graphic
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Hangman

  • Guest
Re: Simple 2x4 ... Not for me ...
« Reply #6 on: October 03, 2005, 06:24:23 PM »
Quote
As for the error, when are you pressing the Enter key & getting the error?
Cab, I run the program ...
After it is completed, I hit the return to get the command involked again.  It then comes to the point where it prompts to enter the size of the timber.  I then press enter to accept the previously used size.  that's where it crashes.



Notice the Timber in Green, that is the first block created.
The one in White is the second block, the program created it, but then crashed when asking to redefine.

Quote
The blocks are not scaled correctly when you bind them?

If I draw a detail at 1" = 1'-0", then another at 1/4" = 1'-0", then another at 1 1/2" = 1'-0" using a generic 2xTimber name of a block.  Then bind these to a 3/4" sheet.  The blocks take the scale of the 3/4" sheet because they all have the same name.  I understand the need for different dimension scale names to avoid this.  And I can't say this happens all the time.  It's actually quite annoying, We've had it happen in two to three different projects out of several, under different circumstances.  I can't actually give you a difinitive answer, this is a "don't mess with it, we don't have the time.  Get a fix and fix it."
So, as Peter mentioned the block collection, I don't want a block collection at all.  This program should draw from scratch any size Block or Timber I need, create the block and insert it with a unique name so it won't interfere with anything else.

Quote
Do the 2X objects have to be blocks?

No, It just makes it easier when editing, easier to move one entity than having to grab two or three.

Peter asked:
Quote
Have you considered a parametric timber creator?

It could create in your blocks collection any size timber on the fly without having to have a thousand predefined blocks?

You may have to elaborate, I don't know just what you mean by "parametric timber creator."  It sounds good though.  :)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Simple 2x4 ... Not for me ...
« Reply #7 on: October 03, 2005, 06:41:12 PM »
... what you mean by "parametric timber creator."

In short, a program would gather the information from the user and then dynamically create the object (or a block definition and an instance) on the fly, rather than constructing a block name from user info and subsequently creating an instance of a predefined block by said name. The latter requires a block library, the former doesn't.

Clear as mud?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Simple 2x4 ... Not for me ...
« Reply #8 on: October 03, 2005, 08:35:26 PM »
Here is a down & dirty solution that draws the object only, no blocks.

Code: [Select]
;;  Revised to eliminate the blocks
;;  Renamed the lisp to ttimber2


;;Written By Pasta - ARW Engineers               Sept 2005
;;
;;This program automatically draws any size timber member or blocking
;;to the correct width and length.
;;
;;
(defun c:ttimber2 (/ oldecho oldos oldortho oldlayr oldpline fn spt pt1 pt2 pt3
                   L1 L2 OLDECHO OLDOS TSIZE W1 W2 ss blnm *error* bname
                  )

  ;; error function & Routine Exit
  (defun *error* (msg)
    (if
      (not
        (member
          msg
          '("console break" "Function cancelled" "quit / exit abort" "")
        )
      )
       (princ (strcat "\nError: " msg))
    ) ; endif

    ;;reset all variables here
    (setvar "plinewid" oldpline)
    (setvar "orthomode" oldortho)
    (setvar "clayer" oldlayr)
    (setvar "cmdecho" oldecho)
    (if (= msg "")
      (command ".undo" "e")
      (command ".undo" "")
    )
    (setq oldecho nil)
    (setq oldos nil)
    (princ)
  ) ;end error function


  ;;  Routine Starts Here
  (command ".undo" "be")

  ;;-- Save the enviornment
  (setq oldecho  (getvar "cmdecho")
        oldos    (getvar "osmode")
        oldortho (getvar "orthomode")
        oldlayr  (getvar "clayer")
        oldpline (getvar "plinewid")
        spt      (getvar "viewctr")
        fn       0
  )
  (setvar "cmdecho" 1)

  ;;-- Default size for symbol
  (or *tsize* (setq *tsize* "24")) ; start with 24


  ;;-- Get User Size for symbol
  (initget
    "12 12B 13 13B 14 14B 16 16B 18 18B 110 110B 112 112B
     22 22B 23 23B 24 24B 26 26B 28 28B 210 210B 212 212B
     33 33B 34 34B 36 36B 38 38B 310 310B 312 312B 314 314B 316 316B
     43 43B 44 44B 46 46B 48 48B 410 410B 412 412B 414 414B 416 416B
     64 64B 66 66B 68 68B 610 610B 612 612B 614 614B 616 616B 618 618B
     86 86B 88 88B 810 810B 812 812B 814 814B 816 816B 818 818B 820 820B 822 822B"
  )
  (setq
    tsize (getkword (strcat "\nEnter size of Timber (i.e. 24 for 2 x 4) <"
                            (substr *tsize* 1 1)
                            " x "
                            (substr *tsize* 2)
                            ">: "
                    )
          )
  )
  (if tsize
    (setq *tsize* tsize)
    (setq tsize *tsize*)
  )

  (setq w1 (atoi (substr tsize 1 1)))
  (setq l1 (atoi (substr tsize 2)))

  ;;  convert to actual dimensions
  (cond
    ((= w1 1)
     (setq w2 (- w1 0.25))
    )
    ((< w1 8)
     (setq w2 (- w1 0.5))
    )
    (t
     (setq w2 (- w1 0.75))
    )
  )


  ;;  convert to actual dimensions
  (if (< l1 8)
    (setq l2 (- l1 0.5))
    (setq l2 (- l1 0.75))
  )


  ;;-- Calculate needed Points
  (setq pt1 (polar spt 0 l2))
  (setq pt2 (polar pt1 (/ pi 2) w2))
  (setq pt3 (polar pt2 pi l2))

  ;;-- Create or prepare Layers
  (if (= (tblsearch "layer" "HATCH") nil)
    (command ".-layer" "new" "HATCH" "color" "8" "HATCH" "")
    (command ".-layer" "thaw" "HATCH" "on" "HATCH" "")
  )

  (if (= (tblsearch "layer" "OBJECT") nil)
    (command ".-layer" "new" "OBJECT" "color" "3" "OBJECT" "")
    (command ".-layer" "thaw" "OBJECT" "on" "OBJECT" "")
  )

  ;;-- Prepare the environment
  (setvar "clayer" "OBJECT")
  (setvar "plinewid" 0)
  (setvar "osmode" 0)

  ;;  Draw the object and add to selection set ss
  (command ".rectang" spt pt2)
  (setq ss (ssget "l"))

  (setvar "clayer" "HATCH")
  (if (wcmatch tsize "*B") ; yes has B
    (command ".line" pt1 pt3 "")
    (progn ; no  B
      (command ".line" spt pt2 "")
      (setq ss (ssadd (entlast) ss))
      (command ".line" pt1 pt3 "")
    )
  )
  (setq ss (ssadd (entlast) ss))

  ;;-- Prepare the environment
  (setvar "orthomode" 0)
  (setvar "osmode" oldos)

  ;;  Added instead of blocks
  (command "._move" ss "" spt pause)
  (setvar "orthomode" 1)
  (command "._rotate" ss "" (getvar "lastpoint") pause)

  ;;-- Reset the environment
  (*error* "")
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Hangman

  • Guest
Re: Simple 2x4 ... Not for me ...
« Reply #9 on: October 04, 2005, 10:17:16 AM »
Thank you CAB, I appreciate your help.
Hey, just out of curiosity, how would you create a group for this ???   Although this would not be a very efficient way of doing things, grouping each timber would make a nasty job.  But I'm curious cause I've tried using the group command but it didn't work very well.  :)