Author Topic: Xdata extraction and String parseing  (Read 2786 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Xdata extraction and String parseing
« on: October 30, 2007, 04:13:15 AM »

Anyone see a way to improve this ?

Code: [Select]
(SETQ ms (CADR (ASSOC -3
                      (ENTGET (CAR (ENTSEL "\nSelect Entity to extract XData : "))
                              '("TSD617_ML")
                      )
               )
         )
)
;|
("TSD617_ML"
 (1000 . "HeaderHeight,40.000000")
 (1000 . "ListHeight,80.000000")
 (1000 . "TextHeight,35.000000")
 (1000 . "Direction,90.000000")
 (1000 . "ml_MemberMark,300.000000,L")
 (1000 . "ml_ItemTag,300.000000,C")
 (1000 . "ml_Quantity,150.000000,C")
 (1000 . "ml_Description,700.000000,L")
 (1000 . "ml_Length,200.000000,R")
 (1000 . "ml_Remark,500.000000,C")
 (1000 . "ml_ItemMass,200.000000,R")
 (1000 . "ml_MemeberMass,200.000000,R")
)

|;
Code: [Select]
(MAPCAR '(LAMBDA (x)
             (MAPCAR '(LAMBDA (s / tmp)
                          (IF (NUMBERP (SETQ tmp (READ s)))
                              tmp
                              s
                          )
                      )
                     (KDUB:string->strlist (CDR x) ", ")
             )
         )
        (CDR ms)
)
;|
(("HeaderHeight" 40.0)
 ("ListHeight"   80.0)
 ("TextHeight"   35.0)
 ("Direction"    90.0)
 ("ml_MemberMark" 300.0 "L")
 ("ml_ItemTag"    300.0 "C")
 ("ml_Quantity"   150.0 "C")
 ("ml_Description" 700.0 "L")
 ("ml_Length"     200.0 "R")
 ("ml_Remark"     500.0 "C")
 ("ml_ItemMass"   200.0 "R")
 ("ml_MemeberMass" 200.0 "R")
)
|;


Generic Library stuff to split the CSV :
Code: [Select]
(DEFUN KDUB:string->strlist (searchstring  delimstring
                             /             tempstring
                             index         tempchar
                             returnlist    delimchars
                            )
    (SETQ delimchars (VL-STRING->LIST delimstring)
          tempstring ""
          index      (1+ (STRLEN searchstring))
    )
    (WHILE (> (SETQ index (1- index)) 0)
        (SETQ tempchar (SUBSTR searchstring index 1))
        (IF (VL-POSITION (ASCII tempchar) delimchars)
            (IF (/= tempstring "")
                (SETQ returnlist (CONS tempstring returnlist)
                      tempstring ""
                )
            )
            (SETQ tempstring (STRCAT tempchar tempstring))
        )
    )
    (IF (/= tempstring "")
        (CONS tempstring returnlist)
        returnlist
    )
)


and no Glenn, 'use C#' doesn't count :-)
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata extraction and String parseing
« Reply #1 on: October 30, 2007, 05:16:18 AM »

and just for completeness,
find all the instances in a dwg.
.. I'm posting this because the SSGET is not 'conventional'
ie ; the -3 key for XDATA is NOT a cons list .. thats just how it works ..

Code: [Select]
;; because the blocks containing the XDATA are anonymous
;; select on the insert and registered app ..
(IF (AND (SETQ ml-ss (SSGET "X" '((0 . "INSERT") (-3 ("TSD617_ML")))))
         (SETQ ml-ents (KDUB:ss->entlist ml-ss))
    )
   
    (FOREACH ml_header ml-ents
        (IF (SETQ ms (CADR (ASSOC -3 (ENTGET ml_header '("TSD617_ML")))))
            (PROGN (SETQ mld
                            (MAPCAR
                                '(LAMBDA (x)
                                     (MAPCAR '(LAMBDA (s / tmp)
                                                  (IF (NUMBERP (SETQ tmp (READ s)))
                                                      tmp
                                                      s
                                                  )
                                              )
                                             (KDUB:string->strlist (CDR x) ", ")
                                     )
                                 )
                                (CDR ms)
                            )
                   )
                   ;; debug assertion
                   (VL-PRIN1-TO-STRING mld )
                   ;;........
                   ;; <snip>
            )
        )
    )
)


Generic Library build an entity list from a selection Set :
Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
(DEFUN KDUB:ss->entlist (ss / i returnval)
    (IF (AND ss (< 0 (SSLENGTH ss)))
        (PROGN (SETQ i 0)
               (REPEAT (SSLENGTH ss)
                   (SETQ returnval (CONS (SSNAME ss i) returnval)
                         i         (1+ i)
                   )
               )
        )
    )
    (IF returnval
        (REVERSE returnval)
        nil
    )
)

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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Xdata extraction and String parseing
« Reply #2 on: October 30, 2007, 06:46:54 AM »
I am just snooping as to why you don’t translate your lists into the IMO more efficient

Code: [Select]
(-3 ("TSD617_ML" (1002 . "{") (1000 . "ml_MemeberMass") (1040 .  200.0) (1000 . "R") (1002 . "}")))

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Xdata extraction and String parseing
« Reply #3 on: October 30, 2007, 07:27:50 AM »
I am just snooping as to why you don’t translate your lists into the IMO more efficient ...

Guessing compactness and ease of translation to / from other formats like csv.

Having said that, there is some merit going the traditional route.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Xdata extraction and String parseing
« Reply #4 on: October 30, 2007, 07:32:05 AM »
Not that I'm likely to come up with anything KB but I will take a more intense look at this in about 10+ hours.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Xdata extraction and String parseing
« Reply #5 on: October 30, 2007, 08:04:49 AM »
I know this isn't what you were looking for but the benefit of opening up code / techniques for review is the tangential stuff that spawns.

Consider if you had stored the data slightly differently, while essentially using the same format. That is --

Instead of --

Code: [Select]
("TSD617_ML"
    (1000 . "HeaderHeight,40.000000")
    (1000 . "ListHeight,80.000000")
    (1000 . "TextHeight,35.000000")
    (1000 . "Direction,90.000000")
    (1000 . "ml_MemberMark,300.000000,L")
    (1000 . "ml_ItemTag,300.000000,C")
    (1000 . "ml_Quantity,150.000000,C")
    (1000 . "ml_Description,700.000000,L")
    (1000 . "ml_Length,200.000000,R")
    (1000 . "ml_Remark,500.000000,C")
    (1000 . "ml_ItemMass,200.000000,R")
    (1000 . "ml_MemeberMass,200.000000,R")
)

You delimited each field as text or numeric --

Code: [Select]
("TSD617_ML"
    (1000 . "\"HeaderHeight\" 40.0")
    (1000 . "\"ListHeight\" 80.0")
    (1000 . "\"TextHeight\" 35.0")
    (1000 . "\"Direction\" 90.0")
    (1000 . "\"ml_MemberMark\" 300.0 \"L\"")
    (1000 . "\"ml_ItemTag\" 300.0 \"C\"")
    (1000 . "\"ml_Quantity\" 150 \"C\"")    ;; truncated (fixed) value
    (1000 . "\"ml_Description\" 700 \"L\"") ;; truncated (fixed) value
    (1000 . "\"ml_Length\" 200.0 \"R\"")
    (1000 . "\"ml_Remark\" 500.0 \"C\"")
    (1000 . "\"ml_ItemMass\" 200.0 \"R\"")
    (1000 . "\"ml_MemeberMass\"200.0 \"R\"")
)

You could then (over simplified for clarity) --

Code: [Select]
(mapcar

   '(lambda ( x / asread )
        (vl-catch-all-apply
           '(lambda ( ) (setq asread (read (strcat "(" x ")"))))
        )
        (if asread asread x)
    )
   
    (mapcar 'cdr
        (cdr
           '("TSD617_ML"
                (1000 . "\"HeaderHeight\" 40.0")
                (1000 . "\"ListHeight\" 80.0")
                (1000 . "\"TextHeight\" 35.0")
                (1000 . "\"Direction\" 90.0")
                (1000 . "\"ml_MemberMark\" 300.0 \"L\"")
                (1000 . "\"ml_ItemTag\" 300.0 \"C\"")
                (1000 . "\"ml_Quantity\" 150 \"C\"")    ;; truncated (fixed) value
                (1000 . "\"ml_Description\" 700 \"L\"") ;; truncated (fixed) value
                (1000 . "\"ml_Length\" 200.0 \"R\"")
                (1000 . "\"ml_Remark\" 500.0 \"C\"")
                (1000 . "\"ml_ItemMass\" 200.0 \"R\"")
                (1000 . "\"ml_MemeberMass\"200.0 \"R\"")
            )
        )
    )   
)

Result --

Code: [Select]
("HeaderHeight" 40.0)
("ListHeight" 80.0)
("TextHeight" 35.0)
("Direction" 90.0)
("ml_MemberMark" 300.0 "L")
("ml_ItemTag" 300.0 "C")
("ml_Quantity" 150 "C")    ;; truncated (fixed) value
("ml_Description" 700 "L") ;; truncated (fixed) value
("ml_Length" 200.0 "R")
("ml_Remark" 500.0 "C")
("ml_ItemMass" 200.0 "R")
("ml_MemeberMass" 200.0 "R")

Some implications: String values could have embedded commas (not relevant in you example because the text fields are to be interpreted as variable names, i.e. no punctuation desired), numeric values could be parsed to ints and reals. To demonstrate the latter I truncated the quantity and description values in the original data.

All likely moot because you a glut of legacy data in the original format but I couldn't help myself.

/thinking out loud sans benefit of sleep or coffee ...
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: Xdata extraction and String parseing
« Reply #6 on: October 30, 2007, 08:43:53 AM »

The Xdata is added by an ARX.
The formatted of the source data is in large beyond my control.
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: Xdata extraction and String parseing
« Reply #7 on: October 30, 2007, 08:49:25 AM »
'kay
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Xdata extraction and String parseing
« Reply #8 on: October 30, 2007, 10:01:05 AM »
The reason I asked is because I am working on something similar, both in C# and lisp.
I thought maybe …      Anyway, as usual your coding is exceptional.