Author Topic: Tell me why I did it like this. :)  (Read 19019 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #15 on: September 23, 2010, 04:38:18 PM »
I didn't say it was a benefit. I was stating that I would never have thought to create the dotted pair list and redefine the command.

Sorry, poor wording on my part, I just didn't understand the motivation to go the vl-remove-et-al route. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Tell me why I did it like this. :)
« Reply #16 on: September 23, 2010, 04:42:21 PM »

I did it because there is
(1) no reason to recreate the list every time the function is called <i.e. evaluate / create the list once, then redefine the function with static data>
(2) not palatable to me to hard code third party enum values <ok to do mine tho>
(3) easy to revise the enum values if additional enum members are added down the road ... etc. :)

I understand those reasons.

I Think I'd have done something like this
Code: [Select]
(Defun Getalignment (Value)
  (Cdr (Assoc Value
              (List (cons acAlignmentLeft "Left")
                    (cons acAlignmentCenter "Center")
                    (cons acAlignmentRight "Right")
                    (cons acAlignmentAligned "Aligned")
                    (cons acAlignmentMiddle "Middle")
                    (cons acAlignmentFit "Fit")
                    (cons acAlignmentTopLeft "TopLeft")
                    (cons acAlignmentTopCenter "TopCenter")
                    (cons acAlignmentTopRight "TopRight")
                    (cons acAlignmentMiddleLeft "MiddleLeft")
                    (cons acAlignmentMiddleCenter "MiddleCenter")
                    (cons acAlignmentMiddleRight "MiddleRight")
                    (cons acAlignmentBottomLeft "BottomLeft")
                    (cons acAlignmentBottomCenter "BottomCenter")
                    (cons acAlignmentBottomRight "BottomRight")
              )
       )
  )
)
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.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Tell me why I did it like this. :)
« Reply #17 on: September 23, 2010, 04:43:36 PM »
A variation?

Code: [Select]
(defun-q GetAlignment ( value / lst )

  (setq lst
    (mapcar
     '(lambda ( enum ) (vl-list* (vl-symbol-value enum) (substr (vl-symbol-name enum) 12)))     
     '(acAlignmentLeft
       acAlignmentCenter
       acAlignmentRight
       acAlignmentAligned
       acAlignmentMiddle
       acAlignmentFit
       acAlignmentTopLeft
       acAlignmentTopCenter
       acAlignmentTopRight
       acAlignmentMiddleLeft
       acAlignmentMiddleCenter
       acAlignmentMiddleRight
       acAlignmentBottomLeft
       acAlignmentBottomCenter
       acAlignmentBottomRight
      )
    )
  )

  (setq GetAlignment
    (cons (car GetAlignment)
      (list
        (cons (quote cdr)
          (list
            (list (quote assoc) (quote value) (list (quote quote) lst))
          )
        )
      )
    )
  )

  (cdr (assoc value lst))
)

(quote quote) lol :D

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #18 on: September 23, 2010, 04:47:19 PM »
I didn't say it was a benefit. I was stating that I would never have thought to create the dotted pair list and redefine the command.

Sorry, poor wording on my part, I just didn't understand the motivation to go the vl-remove-et-al route. :)
Only because I felt it was faster than creating the dotted pair list, then assoc'ing out what was needed.
eg.
Code: [Select]
(defun foo2 (v)
  (cdr (assoc v
              (mapcar
                '(lambda (enum) (cons (eval enum) (substr (vl-symbol-name enum) 12)))
                '(acAlignmentLeft acAlignmentCenter acAlignmentRight acAlignmentAligned
                  acAlignmentMiddle acAlignmentFit acAlignmentTopLeft acAlignmentTopCenter
                  acAlignmentTopRight acAlignmentMiddleLeft acAlignmentMiddleCenter
                  acAlignmentMiddleRight acAlignmentBottomLeft acAlignmentBottomCenter
                  acAlignmentBottomRight
                 )
              )
       )
  )
)
The brilliance is you constructing the dotted pair list and then redefining the routine to included the newly constructed list.
Consider me educated. Thank you.  :-)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Tell me why I did it like this. :)
« Reply #19 on: September 23, 2010, 04:48:41 PM »
Re: vl-remove-et-al approach:

Would (car (vl-member-if ... )) be quicker?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #20 on: September 23, 2010, 04:52:08 PM »
Re: vl-remove-et-al approach:

Would (car (vl-member-if ... )) be quicker?
Quite a bit. I didn't know that one.

Code: [Select]
(defun foo3 (v)
  (if (and (numberp v) (<= 0 (setq v (fix v)) 14))
    (substr (vl-symbol-name
              (car (vl-member-if
                     (function (lambda (e) (eq v (eval e))))
                     '(acAlignmentLeft acAlignmentCenter acAlignmentRight acAlignmentAligned
                       acAlignmentMiddle acAlignmentFit acAlignmentTopLeft acAlignmentTopCenter
                       acAlignmentTopRight acAlignmentMiddleLeft acAlignmentMiddleCenter
                       acAlignmentMiddleRight acAlignmentBottomLeft acAlignmentBottomCenter
                       acAlignmentBottomRight
                      )
                   )
              )
            )
            12
    )
  )
)

Code: [Select]
    (FOO3 2).....1638 / 2.81 <fastest>
    (FOO 2)......4383 / 1.05
    (FOO2 2).....4602 / 1 <slowest>

MP's method is still the powerhouse.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #21 on: September 23, 2010, 04:58:22 PM »
Some more 'purely fun for thought':

Code: [Select]
(defun MakeEnumFunction ( name enums trimfrom / lst )

    (setq lst
        (mapcar
           '(lambda ( enum ) (cons (eval enum) (substr (vl-symbol-name enum) trimfrom)))
            enums
        )
    )

    (eval
        (cons 'defun
            (list name '( value )
                (cons 'cdr
                    (list
                        (list 'assoc 'value
                            (list 'quote lst)
                        )
                    )
                )    
            )
        )
    )    
    
)

Code: [Select]
(MakeEnumFunction
   'GetAlignment
   '(   acAlignmentLeft
        acAlignmentCenter
        acAlignmentRight
        acAlignmentAligned
        acAlignmentMiddle
        acAlignmentFit
        acAlignmentTopLeft
        acAlignmentTopCenter
        acAlignmentTopRight
        acAlignmentMiddleLeft
        acAlignmentMiddleCenter
        acAlignmentMiddleRight
        acAlignmentBottomLeft
        acAlignmentBottomCenter
        acAlignmentBottomRight
    )
    12
)

Code: [Select]
(MakeEnumFunction
   'GetHorizontalAlignment
   '(
        acHorizontalAlignmentAligned
        acHorizontalAlignmentCenter
        acHorizontalAlignmentFit
        acHorizontalAlignmentLeft
    )
    22
)

Code: [Select]
(MakeEnumFunction
   'GetVerticalAlignment
   '(
        acVerticalAlignmentBaseline
        acVerticalAlignmentBottom
        acVerticalAlignmentMiddle
        acVerticalAlignmentTop
    )
    20
)

(GetAlignment 0) >> "Left"

(GetHorizontalAlignment 1) >> "Center"

(GetVerticalAlignment 2) >> "Middle"

:whistle:

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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #22 on: September 23, 2010, 05:01:52 PM »
Most impressive.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #23 on: September 23, 2010, 05:06:56 PM »
For you guys that find this remotely interesting and are into python you might consider reading about functors and closures. :) PS: Thanks Alan/all.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Tell me why I did it like this. :)
« Reply #24 on: September 23, 2010, 06:03:02 PM »
Some more 'purely fun for thought':

Code: [Select]
(defun MakeEnumFunction ( name enums trimfrom / lst )

< .. >
)

(GetAlignment 0) >> "Left"

(GetHorizontalAlignment 1) >> "Center"

(GetVerticalAlignment 2) >> "Middle"

:whistle:




The bonus of the ac.. enumerators being consistantly named ...

Now it's getting interesting !!

Great concept post Michael.
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #25 on: September 24, 2010, 10:33:18 AM »
The bonus of the ac.. enumerators being consistantly named ...

*Gack*

Turns out the TextGenerationFlags are not consistently named because they are the partial union of two enums:

        acTextFlagBackward
        acTextFlagUpsideDown
        acHorizontalAlignmentMiddle
        acHorizontalAlignmentRight

So it would be better to return the full enum member string rather than truncated ones, i.e. "acHorizontalAlignmentMiddle" instead of "Middle". Which I can live with. So, code revised --

Code: [Select]
(defun MakeEnumFunction ( name enums / lst )

    (setq lst
        (mapcar
           '(lambda ( enum ) (cons (eval enum) (vl-symbol-name enum)))
            enums
        )
    )

    (eval
        (cons 'defun
            (list name '( value )
                (cons 'cdr
                    (list
                        (list 'assoc 'value
                            (list 'quote lst)
                        )
                    )
                )
            )
        )
    )

)

Code: [Select]
(MakeEnumFunction
   'GetAlignment
   '(   acAlignmentLeft
        acAlignmentCenter
        acAlignmentRight
        acAlignmentAligned
        acAlignmentMiddle
        acAlignmentFit
        acAlignmentTopLeft
        acAlignmentTopCenter
        acAlignmentTopRight
        acAlignmentMiddleLeft
        acAlignmentMiddleCenter
        acAlignmentMiddleRight
        acAlignmentBottomLeft
        acAlignmentBottomCenter
        acAlignmentBottomRight
    )
)

Code: [Select]
(MakeEnumFunction
   'GetHorizontalAlignment
   '(
        acHorizontalAlignmentAligned
        acHorizontalAlignmentCenter
        acHorizontalAlignmentFit
        acHorizontalAlignmentLeft
    )
)

Code: [Select]
(MakeEnumFunction
   'GetVerticalAlignment
   '(
        acVerticalAlignmentBaseline
        acVerticalAlignmentBottom
        acVerticalAlignmentMiddle
        acVerticalAlignmentTop
    )
)

Code: [Select]
(MakeEnumFunction
   'GetTextGenerationFlag
   '(
        acTextFlagBackward
        acTextFlagUpsideDown
        acHorizontalAlignmentMiddle
        acHorizontalAlignmentRight
    )
)

(GetAlignment 0) >> "acAlignmentLeft"

(GetHorizontalAlignment 1) >> "acHorizontalAlignmentCenter"

(GetVerticalAlignment 2) >> "acVerticalAlignmentMiddle"

(GetTextGenerationFlag 4) >> "acTextFlagUpsideDown"

Great concept post Michael.

Thank you Kerry.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Maverickฎ

  • Seagull
  • Posts: 14778
Re: Tell me why I did it like this. :)
« Reply #26 on: September 24, 2010, 10:40:21 AM »
Sometimes I feel smarter just "hanging around" you guys.

Usually I just feel dumberer.

Carry on.  :-D

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #27 on: September 24, 2010, 10:49:52 AM »
Here's a general StripEnumPrefix function one can use after the fact ...

Code: [Select]
(defun StripEnumPrefix ( enum / result )

    (vl-some
       '(lambda ( pair )
            (if (wcmatch enum (car pair))
                (setq result (substr enum (cdr pair)))
            )
        )
       '(   ("acAlignment*" . 12)
            ("acHorizontalAlignment*" . 22)
            ("acVerticalAlignment*" . 20)
            ("acTextFlag*" . 11)
            [color=green];; add additional filters to suit ...[/color]
        )
    )
    
    (if result result enum)

)

(StripEnumPrefix "acTextFlagBackward") >> "Backward"

(StripEnumPrefix "acHorizontalAlignmentMiddle") >> "Middle"

(StripEnumPrefix "acVerticalAlignmentBottom") >> "Bottom"

Kerry(Mav) you be funny. :D
« Last Edit: September 24, 2010, 10:55:35 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Tell me why I did it like this. :)
« Reply #28 on: September 24, 2010, 03:37:08 PM »
Perhaps for a touch grammatical house-keeping  :wink:

Code: [Select]
(defun _SplitbyCaps ( s / _f )

  (defun _f ( s )
    (if (wcmatch s "*[A-Z]*")
      (strcat (if (< 64 (ascii s) 91) " " "") (substr s 1 1) (_f (substr s 2)))
      s
    )
  )

  (strcat (substr s 1 1) (_f (substr s 2)))
)

Code: [Select]
(_SplitByCaps "MiddleCenter")
"Middle Center"

Yeah I was bored.   :-D

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Tell me why I did it like this. :)
« Reply #29 on: September 24, 2010, 05:10:25 PM »
Lee,
This would be a far faster method (NOTE: I reformatted this function in the VLIDE for Kerry :D ).
Code: [Select]
(defun SplitbyCaps (mstr / imstr)
  (setq imstr (vl-string->list mstr))
  (apply
    (function strcat)
    (cons
      (chr (car imstr))
      (mapcar
    '(lambda (x)
       (if (< 64 x 91)
         (strcat " " (chr x))
         (chr x)
       )
     )
    (cdr imstr)
      )
    )
  )
)

Quote
Benchmarking ...........Elapsed milliseconds / relative speed for 256 iteration(s):

    (SPLITBYCAPS SUPERLONGSTRING).......1670 / 54.24 <fastest>
    (_SPLITBYCAPS SUPERLONGSTRING).....90574 / 1.00 <slowest>

Tested on a string:
_$ (strlen superlongstring)
6176
_$


I will need time to read and re-read this thread but my initial impression is: "a code {re}generator". would that be correct?



EDIT: Added Benchmark test.
« Last Edit: September 24, 2010, 05:16:16 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org