Author Topic: Block replacement lisp  (Read 30579 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Block replacement lisp
« Reply #30 on: March 01, 2006, 05:36:57 PM »
I really hate to get entangled in things like this, especially when there are people I like and respect on both sides of the argument, < .. >

Yep, It's a tough decision !

I try to apply Asimovs 3 Laws of Robotics , but sometimes a blood rush comes over me ...

Isaac Asimov's "Three Laws of Robotics"
A robot may not injure a human being or, through inaction, allow a human being to come to harm.
A robot must obey orders given it by human beings except where such orders would conflict with the First Law.
A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Block replacement lisp
« Reply #31 on: March 01, 2006, 05:43:55 PM »
Sup everyone.

Check this out: http://www.dianaskitchen.com/page/beans/madame.htm

EDIT: Fat fingers hit the (tab-enter)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Block replacement lisp
« Reply #32 on: March 01, 2006, 06:07:20 PM »
...*Ptthht!* I give [you] the answer to all of the probems mentioned in this thread and [you] just ignore me. Fine maybe some original quotes will help.

o  The only way to win an argument is to avoid one. -- Se7en

o  All arguments between two people have a minimum of at least two loosers -- Se7en

o  To have an argument with oneself is much more rewarding; at least then you
   stand a chance coaxing the other party to your way of thinking. -- Se7en

o  Two arguee's walk into a club. ...Good maybe now we can get some peace. --Se8en

o  How many arguee's does it take to change a light bulb? ...ONE! You got a fricken
   problem with that?!?! -- Se8en
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CADaver

  • Guest
Re: Block replacement lisp
« Reply #33 on: March 01, 2006, 10:35:22 PM »
VERY old functions REPL askes for block name, REPLS ask to select a block to match:

Code: [Select]
;;;;;;;;;
;;Falcon Design Services, inc. 1992
;;;;;;;;;
(defun c:REPL (/ ENT1 BL1 NWNM OLD ODNM)
(command "undo" "begin")
 (prompt "Select blocks to replace: ")
 (setq ENT1 (ssget))
 (setq NEWBL (getstring "\nEnter new block name: "))
 (command "insert" NEWBL nil)
 (setq N (sslength ENT1))
 (setq I 0)
 (repeat N
  (setq BL1 (entget (ssname ENT1 I)))
  (setq NWNM (cons 2 NEWBL))
  (setq OLD (assoc 2 BL1))
  (setq ODNM (cdr OLD))
  (entmod (subst NWNM OLD BL1))
  (setq I (1+ I))
 )
(command "undo" "end")
 (prin1)
)


(defun c:REPLs (/ ENT1 BL1 bl2 OLD ODNM)
(command "undo" "begin")
 (prompt "\nSelect Replacement Block: ")
 (setq bl2 (cdr (assoc 2 (entget (car (entsel))))))
 (prompt "Select blocks to replace: ")
 (setq ENT1 (ssget))
 (setq N (sslength ENT1))
 (setq I 0)
 (repeat N
  (setq BL1 (entget (ssname ENT1 I)))
  (setq NWNM (cons 2 bl2))
  (setq OLD (assoc 2 BL1))
  (setq ODNM (cdr OLD))
  (entmod (subst NWNM OLD BL1))
  (setq I (1+ I))
 )
(command "undo" "end")
 (princ)
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Block replacement lisp
« Reply #34 on: March 01, 2006, 11:59:00 PM »
Ok .. lets look at this from a fresh perspective. I have not followed this thread too close, but it appears as though there is a "bash joe" party going on. This certainly does not follow in the spirit of the swamp that some have touted as being the appropriate behavior.
Upon examination of Joe's comments, (there were several), the basic facts which are ...
  • Joe fails to see what is interesting in this particular piece of code
  • The code is bloated
  • Expresstools has a very similar function
  • Code from places OTHER THAN TheSwamp and/or the Autodesk forum is GENERALLY trash
  • Just because a function does what it says it should do does not mean it SHOULD be used or that it is worth using
  • "Pete's Magic Block Replacer" seems a bit like the author may have been a cad manager or someone more interested in protecting one's job than writing good code
  • Joe stands by his comments regardless of whether "Pete" sees it or not.

Now to offer a bit of commentary on these basic "facts" as they have been presented ...
#1 - This is a simple commentary on his personal thoughts about this code ... I have seen similar comments from just about every seasoned programmer that frequents this forum. To speak out in this particular instance is hypocritical for anyone who has ever made such similar statements. I will refrain from name calling as it solves nothing ... you know who you are.

#2 - This statement is 100% accurate and to suggest otherwise helps no one ... however to suggest that a piece of code is bloated will allow those who are learning to code, that there are much more efficient methods that may be used to accomplish a task. This could be correctly used as a fine example of how code may become bloated , particularly when the programmer is inexperienced.

#3 - This is absolutely true ... and yes I do realize that many people may not have Expresstools or may elect to not use them for whatever reason. I personally only use 4 or 5 of them although I have the full suite. For the remaining items I have built my own custom tools, much like many of the fine folks who frequent TheSwamp, however, I suspect that most folks who purchase AutoCAD do have the Expresstools, as they have been included in every release of AutoCAD since R2000 (as far as I can tell) up to and including R2004 ... R14 is the only version that used Bonus tools ... and they were abismal ... but that is merely my own opinion .. others may vary.

#4 - This one is a bit shaky as I have seen some fine examples of code on websites other than TheSwamp and/or Autodesk forums ... but the author did temper the statement with the disclaimer "GENERALLY" ... thus he may not consider all code from other sources as trash, but then again this is his particular commentary ... yours may differ,  nothing wrong with that .. it is just different that's all ...

#5 - Ok .. here is where it gets nasty ... I want everyone here who uses an alternate O/S or web browser, (i.e. no Windows or Internet Explorer), that takes offense to this comment to sincerely do some soul searching ... imagine this comment ... "Just because Windows/IE does what is says it is supposed to do, does not mean it should be used or that it is worth using" ... Now step back and re-examine your position. I suspect that a whole lot of Swampers use alternative browsers .. i.e. Firefox,  Mozilla, Opera ... heck, I even wrote my own browser ... and if Mark goes back in his logs he may even find it there .. I called it "Swamp Browser" ... still use it on occasion too ... more of an exercise than a desire to create a better browser. It was fast and sleek ... and unencumbered by a majority of the security flaws of other browsers ... but just because I have it does not mean that it is desirable to use it .. or that it should be used. Enough about this ... I think I have gotten my point across.

#6 - Ok ... first off,  there is nothing magic about it ... although it may seem like it to a layman .. thus I got the same exact feeling as did Joe. The author is someone who feels self-important (as most cad managers would likely feel) you know the kind that are self absorbed ... I know you do as I have seen the comments from you folks ... the ITwit and the stupid CAD manager threads pop up almost weekly, and have for a long time. The mere fact that the writer of the code alludes to the fact that it is somehow magic or states unequivocally that it is "Pete's" says a whole lot about how he wishes to portray himself. To me that screams "YOU CAN'T DO WITHOUT ME .... BUWAHAHAHAHA" thus the end user being unfamiliar with coding would indeed feel that the person is valuable and deserves to have their job protected, regardless of whther the code is bloated or follows any kind of good programming practices. There is nothing inherently wrong with this statement ... it is what it is ... and many of us HAVE portrayed ourselves as "invaluable" to our respective employers ... all for the sake of self importance. If the shoe fits, wear it... but please don't pretend that "Pete", whomever he may be, is not (or at very least was not at the time of the code writing) a person with a percocious attitude. To do so would be completely inaccurate in my humble opinion.

#7 - Finally, while some of you have chosen to take offense to this comment, I personally find it refreshing ... I do not need to guess where he stands ... this piece of code could have just as well been something I produced many years ago before I would have known better (not this particular piece, but you get the picture) and I would prefer that someone be completely open and honest when dealing with me ... Do YOU like to be treated differently by people when they know you are around? i.e. would you prefer that your co-worker be open about the fact that they think you are an idiot or would you prefer that they be nice to you while you are standing there, then talk crap about you when you leave ... this kind of puts a different perspective on it now doesn't it. I don't know anyone who would prefer to be lied to or at very least be patronized by people who have a dislike for them or their work habits. Being honest and open about ones feelings is perhaps the one last thing that we can do ... now of course I am not advocating wholesale bashing, but if it looks like a duck, walks like a duck and quacks like a duck, it just might be a duck ... and I will be hard pressed to call it a turkey.

Ok .. if you have gotten this far in my unbelievably bloated commentary (dangit I can be long winded) you should note that while I would not have made those comments myself, I will stand behind the honesty in which Joe made them. If they had been inflamitory or derogatory I would have been the first to delete it ... but the mere fact that nobody saw fit to delete the post means that it certainly did not fall outside the bounds of the spirit of the swamp, since it would have surely been deleted had it done so. Personally I find the ensuing commentary much ado about nothing and it serves no purpose except to prompt people to take sides ... and as it stands here now, I am ashamed that I feel compelled to engage in perpetuating this discussion.

Where oh where has the time gone when people could engage in honest and open debate without getting their feelings hurt.

And here is a simple code bit that allows for replacement of any selected blocks with the block name of the users choice ... no fancy browse for files,  no configurable options ... simply if you select the block you can change it with another block already defined in the drawing.
Code: [Select]
(PRINC "\n      -- Change Block Version 2.3 -- start command with --> CB ")
(princ "\n      -- Copyright (C) 1997-2006 by K.E. Blackie ")
(princ "\n")
(DEFUN C:CB()
 (setq ss (ssget))
 (prompt"\nNew Block Name: ")
 (setq new_value (getstring t))
 (setq index 0)
 (repeat (sslength ss)
  (setq e (entget(ssname ss index)))
  (setq as (assoc 2 e))
  (setq e (subst (cons 2 new_value) as e))
  (entmod e)
  (setq index (+ 1 index))
 )
 (princ)
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Block replacement lisp
« Reply #35 on: March 02, 2006, 07:39:46 AM »
You can't tell me with a straight face you that if Mark Thomas posted a routine all you mods would let this post to stand without rebuke --

Maybe I don't get it, but I fail to see what's interesting about Mark's bloated code.

Mark's Magic ... ? That should tell you that Mark is probably a CAD manager more interested in protecting his job than writing good code. If Mark is around, I won't retract that statement.


And I'm saying that everyone deserves that courtesy.

To add to what I've already said on this topic -- people should feel comfortable posting to the swamp without fear of ridicule, insult and character assassinations based on wild assed assertions and assumptions.

As for the hypocritical suggestion Mr. Blackie, I stepped down as the swamp's admin because of this very issue -- and you know that. It doesn't mean I'm perfect, far from it -- if I were I wouldn't have felt compelled to step down nor would I had said previously in this thread "While it is a high bar, one that I have publically struggled with myself, it is a goal worth aspiring to ...".

This is not a bash Joe party. That's the last thing I want, and no one who chimed in to offer their opinion provided more than statements about their own philosophy or a preference for a positive, edifying type environment. There's no mob mentality going on here Keith so just put that notion away. I have valued Joe's expertise, perspective and contributions over the years. That hasn't changed nor will it, but I could not ignore my conscience and let that post stand without an accounting.

Finally this not about a failure to acknowledge inferior code. Horse pucky. You can fully address all the shortcomings of programming technique without being insulting, belittling and abrasive.

It just takes a little more effort.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Block replacement lisp
« Reply #36 on: March 02, 2006, 08:38:53 AM »
For the record, I wanted to stay out of this.

I found Joe's comments a bit harsh myself and not what I would like to see here on theswamp. The last thing I want is for someone new to programming or theswamp to be ashamed of posting their code.

I didn't see the previous comments as "Joe bashing".

How the heck can you guys tell what type of person Pete is, not to mention his his job title, from two words, "Pete's Magic" ? *grin*

Quote
...but the mere fact that nobody saw fit to delete the post means that it certainly did not fall outside the bounds of the spirit of the swamp, since it would have surely been deleted had it done so...

The thought did cross my mind. I was late to the party myself so I left said post in tact. If this conversation goes much further in the direction it seems to be going you can expect the whole thread to disappear.
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Block replacement lisp
« Reply #37 on: March 02, 2006, 12:40:00 PM »
< ... >
And here is a simple code bit that allows for replacement of any selected blocks with the block name of the users choice ... no fancy browse for files,  no configurable options ... simply if you select the block you can change it with another block already defined in the drawing.
Code: [Select]
(PRINC "\n      -- Change Block Version 2.3 -- start command with --> CB ")
(princ "\n      -- Copyright (C) 1997-2006 by K.E. Blackie ")
(princ "\n")
(DEFUN C:CB()
(setq ss (ssget))
(prompt"\nNew Block Name: ")
(setq new_value (getstring t))
(setq index 0)
(repeat (sslength ss)
  (setq e (entget(ssname ss index)))
  (setq as (assoc 2 e))
  (setq e (subst (cons 2 new_value) as e))
  (entmod e)
  (setq index (+ 1 index))
)

)

Keith, Is this what you consider to be an example of good, acceptable code style. ?

I ask because I think that most people learn by example, or osmosis if you like, and I wanted to know if your post was meant as a real example.


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Block replacement lisp
« Reply #38 on: March 02, 2006, 01:26:28 PM »
The example was meant as just that an example ... it certainly may be improved upon, however drastic or slight is entirely up to the person looking at the code. For the intent, it works as described, however, just because it works does not mean that is should be used or that it is desirable to do so. All code is provided with that understanding.

To critique my own code, I might point out a few flaws that are neither desirable or good coding practice ...
1. None of the vars are localized, thus it may crash with other software.
2. The code is not commented, thus the novice may not readily understand the code
3. The use of unrequired variables is not desirable in working code ... but may be helpful while learning

I am sure there are others, and if you like you may point them out ... I have big shoulders and I can take it ... but if you would like to see an example of what I would consider as acceptable coding and correction of the above items I describe, given the above example of simplification of block replacement, see below:



;;; Change Block Version 2.3.1
;;; Copyright (C) 1997-2006 by K.E. Blackie
;;;
;;; You may copy, modify, use or distribute this code provided that
;;; the copyright notice remains in place and that no monetary
;;; consideration be given for the use or transfer of such code.
;;;
;;;
;;;-------------------------------------------------
;;; The function CB will replace a selected block reference(s)
;;; with another block reference that is already defined in
;;; the drawing. All attributes will be retained until/unless
;;; attsync is used.
;;;
(defun C:CB( / intCounter lstOldReference ssBlocks strNewBlockName )
   ;; prompt the user to select items, filtering for blocks only
   (setq ssBlocks (ssget '((0 . "INSERT"))))
   ;; ask the user for a replacement block name
   (setq strNewBlockName (getstring t "New Block Name: "))
   ;; initialize the counter
   (setq intCounter 0)
   ;; step through the selection set of blocks
   (repeat (sslength ssBlocks)
      ;; get the entity list for further manipulation
      (setq lstOldReference (entget(ssname ssBlocks intCounter)))
      ;; substitute the new block name for the old block name and update the drawing
      (entmod (subst (cons 2 strNewBlockName)(assoc 2 lstOldReference) lstOldReference))
      ;; increment the counter
      (setq intCounter (1+ intCounter))
   ); _end of repeat
   ;; clean up nicely .. i.e. don't leave anything on the command line
   (princ)
); _end of defun
;;; Notify the user of the current version and command call
(princ "\n      -- Change Block Version 2.3.1 -- start command with --> CB ")
;;; Display copyright notice
(princ "\n      -- Copyright (C) 1997-2006 by K.E. Blackie ")
;;; load with a nice clean command line ... it is better that way
(princ "\n")




Now while this code is nicely formatted, commented localized and has variables that the user can readily identify (and what they should contain), the basics of the code is the same.

Personally I feel .. teach how to code first, then work on semantics
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Block replacement lisp
« Reply #39 on: March 02, 2006, 01:40:16 PM »
How about a Prompt to guide the user into the Selection of the Blocks ?

And a reminder that Attsync is/may be needed ?

And Why do you comment every line ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Block replacement lisp
« Reply #40 on: March 02, 2006, 01:44:31 PM »
I ask because I think that most people learn by example, or osmosis if you like...

Interesting theory Kerry, if I understand you correctly.

So you're saying if people read enough books or other material on programming lisp then eventually they will know how to write lisp without actually opening a text editor?
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Block replacement lisp
« Reply #41 on: March 02, 2006, 01:52:49 PM »
While that is true to an extent, punching out code IS the best teacher.

Some of it may be horrible at first, but as knowledge improves, so does the code.

The problem with learning by osmosis is that if the examples are not good the derived code will be less than good, and the coder may not know the difference. We have to learn to question every line we write < or read >.

The real intent of my comment was to express my belief that we should be a little carefull with the code we post as examples and never shy from judgement our our own, or sample code.
« Last Edit: March 02, 2006, 02:02:08 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Block replacement lisp
« Reply #42 on: March 02, 2006, 02:48:17 PM »
How about a Prompt to guide the user into the Selection of the Blocks ?

And a reminder that Attsync is/may be needed ?

And Why do you comment every line ?

All valid questions .. it is presumed that the user will know what the program is supposed to do. After all, they must load the program and enter the command name at the command line. If the user does not know that they must select blocks with a block replacement utility, then they should not be using the code.

Remember the use of code should be to improve performace and make a users life a bit easier. It should not be used to make bad users good. No amount of coding can make up for incompetence.

Commenting every line does make for alot of writing and can overwhelm the code, as is exhibited above, however, it is necessary in many instances for the complete understanding of the code, particularly for a novice. If we are not talking about a novice coder, then the user wouldn't be looking too closely at the code to learn from .. thus the learning by viewing debate would not apply and we would be discussing a completely different scenario ... i.e. what would be appropriate learning material for a novice/intermediate/expert coder.

Now the issue with attsync ... as I have said ... just because the program does what it is supposed to do does not mean it is desirable to use it. The fact of the matter is, if the user replaces a block with any block replacement program, they will all require attsync if attributes are part of the block, either that or the code must update the attributes. Either way, the user will be required to act upon the block after updating.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Block replacement lisp
« Reply #43 on: March 02, 2006, 03:14:34 PM »
Thanks for clarifying that Keith.

In line with your comments and intent,
and as an alternate example of style < retaining the same essential functioning code >,
Here's another alternative which may read a little better for readers of all skill levels.

Code: [Select]
;;; Change Block Version 2.3.1
;;; Copyright (C) 1997-2006 by K.E. Blackie
;;;
;;; You may copy, modify, use or distribute this code provided that
;;; the copyright notice remains in place and that no monetary
;;; consideration be given for the use or transfer of such code.
;;;;
;;;;-------------------------------------------------
;;; The function CB will replace a selected block reference(s)
;;; with another block reference that is already defined in
;;; the drawing. All attributes will be retained until/unless
;;; attsync is used.
;;;
;;
;; Modified for style by kwb@theswamp 20060303
;;

(defun C:CB (/ intCounter lstOldReference ssBlocks strNewBlockName)
  ;;
  ;; prompt the user to select items, filtering for blocks only
  ;;
  (prompt "\nSelect BLOCKS to be replaced.")
  (setq ssBlocks        (ssget '((0 . "INSERT")))
        strNewBlockName (getstring t "\nReplacement Block Name: ")
        intCounter      0
  )

  ;; step through the selection set of blocks
  ;; get the entity list and substitute the new block name for the old block name
  ;; and update the entity with entmod
  ;;
  (repeat (sslength ssBlocks)
    (setq lstOldReference (entget (ssname ssBlocks intCounter)))
    (entmod (subst (cons 2 strNewBlockName)
                   (assoc 2 lstOldReference)
                   lstOldReference
            )
    )
    (setq intCounter (1+ intCounter))
  )

  ;; Remind about Syncronisation .. don't leave anything on the command line
  ;;
  (princ "\nAttsync may be required for Attributed Blocks")
  (princ)
)

;;; Notify the user of the current version and command call
;;; Display copyright notice
 
(princ "\n      -- Change Block Version 2.3.1 -- start command with --> CB ")
(princ "\n      -- Copyright (C) 1997-2006 by K.E. Blackie ")
(princ "\n")

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Block replacement lisp
« Reply #44 on: March 02, 2006, 03:26:55 PM »
Indeed ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie