Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with LISP to sort CANNOSCALE list

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
3dwannab
2954 Views, 12 Replies

Help with LISP to sort CANNOSCALE list

I need help to sort the list of a CANNOSCALE LISP i created from searching the internet on how to do so. It creates and sorts them fine if no CANNOSCALES are not in use. Trouble is if 1:50 and 1:100 scales exist then it sorts them like:

 

"1:50"
"1:100"

"1:1"
"1:2"
"1:5"
"1:10"
"1:20"
"1:250"
"1:500"
"1:1000"
"1:2500"

 

I need:

"1:1"
"1:2"
"1:5"
"1:10"
"1:20"

"1:50"
"1:100"
"1:250"
"1:500"
"1:1000"
"1:2500"

 

Thanks. PS I'm a LISPING noob.

Tags (2)
12 REPLIES 12
Message 2 of 13
_Tharwat
in reply to: 3dwannab

Check this out ..

 

(setq l '("1:50" "1:100" "1:1" "1:2" "1:5" "1:10" "1:20" "1:250" "1:500"  "1:1000" "1:2500"  ))

(vl-sort l '(lambda (j k) (< (read (substr j 3)) (read (substr k 3)))))

 

Message 3 of 13
3dwannab
in reply to: _Tharwat

CANNOSCALE list.jpg

 

Thanks for your help but I can't get it to work. See image. The list doesn't appear alphabetically if 1:250 scale is in use. See attached .lsp file. Maybe I'm putting the code in the wrong place?

Message 4 of 13
3dwannab
in reply to: _Tharwat

Hi Tharwat,

 

I managed to get it to work but with what seems like an easier method to understand. Especially for a begineer like me.

 

With the code:

(Command: (dos_strsort x -1)
("1:1" "1:2" "1:5" "1:10" "1:20" "1:50" "1:100" "1:250" "1:500" "1:2500")

 See link. Pretty usefull stuff 🙂

 

Attached its the final script. It's coming back with errors saying "; error: no function definition: DOS_STRSORT"

 

It would be nice to get that message rid of. But all in all the script works fine. Thanks again for your help.

Message 5 of 13
Kent1Cooper
in reply to: 3dwannab


@3dwannab wrote:

.... 

With the code:

(Command: (dos_strsort x -1)
("1:1" "1:2" "1:5" "1:10" "1:20" "1:50" "1:100" "1:250" "1:500" "1:2500")

 See link. Pretty usefull stuff 🙂

 

Attached its the final script. It's coming back with errors saying "; error: no function definition: DOS_STRSORT"

 

It would be nice to get that message rid of. But all in all the script works fine. Thanks again for your help.


First obvious question:  is DOS_STRSORT loaded?

 

Other issues [I don't have the ScaleListEdit command to test with, but there are some things I noticed]:

 

The word command: in there has nothing to do with proper Lisp syntax.

 

You have not put any list of strings into the 'x' variable that you are asking (dos_strsort) to sort.  I suspect you would get that from the scale list somehow.  What the relationship between sorting that list and the "Exit" from the ScaleListEdit command is, I can't say without the command to test, but I'm guessing the sorting operation isn't viable input to that command.

 

I think your ("1:1" "1:2" ... etc. list does not belong in the code, but is just the returned list from your sample sorting operation.  [It's already in order, so if that's the list, you don't need to sort it.]  If a list like that is a viable input to the ScaleListEdit command, then I assume it should be just what is returned by the (dos_strsort) function.

Kent Cooper, AIA
Message 6 of 13
BlackBox_
in reply to: Kent1Cooper

The first offering may throw an error for custom scales (i.e., 10:10000, etc.), as it's hard-coded to only extract a single digit on the left side of the ":" delineator:

 

_$ (setq l '("1:50" "1:100" "1:1" "1:2" "10:10000" "1:5" "1:10" "1:20"
          "1:250" "1:500" "1:1000" "1:2500"
         )
)
("1:50" "1:100" "1:1" "1:2" "10:10000" "1:5" "1:10" "1:20" "1:250" "1:500" "1:1000" "1:2500")
_$ (vl-sort l
         '(lambda (j k) (< (read (substr j 3)) (read (substr k 3))))
)
; error: bad argument type for compare: 5 :10000
_$ 

 

 

... And while the latter offering (DOSLIB's dos_strsort function) works, a dependency is required that some may not like, or even have permissions to install... I happen to enjoy the benefit of using DOSLIB personally.

 

In any event, for those who do/can not use DOSLIB, here's one alternative to acheive the same as calling dos_strsort with the -1 parameter:

 

(vl-load-com)

(defun _SortScalesList (l / _GetScaleItems scales item)
  ;; Example: (_SortScalesList l)
  
  (defun _GetScaleItems (strScale / i)
    (if (setq i (vl-string-search ":" strScale))
      (cons (atoi (substr strScale 1 i))
            (atoi (substr strScale (1+ (1+ i))))
      )
    )
  )

  (foreach scale (mapcar '_GetScaleItems l)
    (if (setq item (assoc (setq unit (car scale)) scales))
      (setq
        scales (subst
                    (cons unit (append (cdr item) (list (cdr scale))))
                    item
                    scales
                  )
      )
      (setq scales (cons (cons unit (list (cdr scale))) scales))
    )
  )
  (apply 'append
         (mapcar
           '(lambda (scale / unit)
              (setq unit (itoa (car scale)))
              (mapcar '(lambda (x) (strcat unit ":" x))
                      (mapcar 'itoa (vl-sort (cdr scale) '<))
              )
            )
           (vl-sort scales '(lambda (x y) (< (car x) (car y))))
         )
  )
)

 

... Result from console:

 

_$ (setq l '("1:50" "1:100" "1:1" "1:2" "10:10000" "1:5" "1:10" "1:20"
          "1:250" "1:500" "1:1000" "1:2500"
         )
)
("1:50" "1:100" "1:1" "1:2" "10:10000" "1:5" "1:10" "1:20" "1:250" "1:500" "1:1000" "1:2500")
_$ (_SortScalesList l)
("1:1" "1:2" "1:5" "1:10" "1:20" "1:50" "1:100" "1:250" "1:500" "1:1000" "1:2500" "10:10000")
_$ 

 

 

HTH



"How we think determines what we do, and what we do determines what we get."

Message 7 of 13
Ian_Bryant
in reply to: 3dwannab

Hi,

as you have discovered resetting the scalelist using the -scalelistedit command.

when there are existing annotative objects in the dwg, puts the scales of these

objects at the top of the scalelist.

To get the scalelist to appear as you want it, you then have to manipulate

the dwg's ACAD_SCALELIST dictionary.

The attached file does this and also allows for the fact that the dwg may contain

objects that have different scales to the ones on your list.

Ian

 

Message 8 of 13
3dwannab
in reply to: Ian_Bryant

You Sir are a legend amoung men. I'm going to study this and try figure out a few things. Sorry to Kent1cooper and BlackBoxCAD but I couldn't manage to get your solutions to work.

This has made my day. Thank you.

Message 9 of 13
Kent1Cooper
in reply to: BlackBox_


@BlackBoxCAD wrote:

The first offering may throw an error for custom scales (i.e., 10:10000, etc.), as it's hard-coded to only extract a single digit on the left side of the ":" delineator:

....


Though we here in the USA left the Empire over 200 years ago, for whatever reason, we're one of the few places left in the world where Imperial measurements are still used.  So I don't use metric scales [I wish I could, as I did for a semester of Architecture School that I spent in Rome], but I'm curious about this.  Is a custom scale like that something that needs to be accounted for?  After all, we're not talking about ratios generically, but drafting scales.  Are metric scales ever expressed in terms other than 1-to-something?  That example is the same as 1:1000, but I can certainly imagine something that wouldn't be equivalent to a standard scale, and wouldn't be reducible to 1-to-something terms, at least not with an integer after the colon, e.g. something like 3:1 or 10:125.  Is that kind of thing ever done?

Kent Cooper, AIA
Message 10 of 13
BlackBox_
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@BlackBoxCAD wrote:

The first offering may throw an error for custom scales (i.e., 10:10000, etc.), as it's hard-coded to only extract a single digit on the left side of the ":" delineator:

....


Though we here in the USA left the Empire over 200 years ago, for whatever reason, we're one of the few places left in the world where Imperial measurements are still used.  So I don't use metric scales [I wish I could, as I did for a semester of Architecture School that I spent in Rome], but I'm curious about this.  Is a custom scale like that something that needs to be accounted for?  After all, we're not talking about ratios generically, but drafting scales.  Are metric scales ever expressed in terms other than 1-to-something?  That example is the same as 1:1000, but I can certainly imagine something that wouldn't be equivalent to a standard scale, and wouldn't be reducible to 1-to-something terms, at least not with an integer after the colon, e.g. something like 3:1 or 10:125.  Is that kind of thing ever done?


Hi Kent,

 

Admittedly I used a simple factor of 10, as you've already pointed out, for demonstration purposes only. Conceptually though I feel this methodlogy (of extracting the prefix, and suffix about the delineator) is preferable over a hard-coded SUBSTR value.

 

After posting that sample, I wondered how it might perform using Archiectural scales instead, but have not yet had an opportunity to test for myself.

 

 

 

Is it something that needs to be accoundet for... No, absolutely not.

 

I only pointed it out, as it happens to be an issue I've encountered, albeit one stemming from poor CAD proactices to my mind, but it happened, and so I thought I'd offer a kludge to account for it (outside of DOSLIb).

 

I too reside and predominantly work on projects here in the U.S., but by nature of working for an international design firm, one is seldom able to rely on their own local practices as a given. That's my world, and certainly should not dictate others who suffer the benefit of being able to have that level of both consistency, and dependability... I wish I had that.

 

Now add in that I recently joined ADN, and now have to make my offerings (at least the one's I intend to seek being published at Autodesk Exchange, and not all per-se) as 'generic' or perhaps more correctly 'inclusive' as they can be, I'm finding areas where I now consider these sort of options where I would not otherwise for my own work.

 

That said, to come full circle to the questions of accounting for various criteria... I'd ultimately fall back on the generalization of "If it is permissible as input, then it _should_ be accounted for."

 

... That at least declares the (in the case my) intent, without an unreasonable expectation that everything _is_ actually accounted for... If that makes (more?) sense.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 11 of 13
csalvian
in reply to: Ian_Bryant

Any chance this LISP has been fleshed out for Imperial scale lists?
Message 12 of 13
3dwannab
in reply to: 3dwannab

I found a file that the scale list that cannot be sorted. I couldn't get it fixed so I'll leave it here for anyone that could perhaps see what the issue is.

 

Just after noticing that the 1:1000 named scale has 1:1 as its properties and it's greyed out also. Maybe that will help troubleshoot the issue.

 

3dwannab_0-1661464549287.png

 

 

Message 13 of 13
3dwannab
in reply to: csalvian

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost