Author Topic: Add Data Link through lisp  (Read 7419 times)

0 Members and 1 Guest are viewing this topic.

jvillarreal

  • Bull Frog
  • Posts: 332
Add Data Link through lisp
« on: December 07, 2011, 10:52:54 AM »
Code: [Select]
(AddDataLink "Test" nil)

First argument is the Data Link Name, the second argument is the file path.

Can anybody help determine what dxf 310 is/how to generate it?
Code: [Select]
  (cons 300 "ACEXCEL_SOURCEDATE")
  (cons 301 "DATAMAP_VALUE")
  (cons 93 2)
  (cons 90 8)
  (cons 92 16)
  (cons 310 "DB070C00020006000D00280036000000")
  (cons 94 0)
  (cons 300 "")
  (cons 302 "")
  (cons 304 "ACVALUE_END")

**Edit**
Removed *some* useless data from code.
« Last Edit: December 07, 2011, 04:01:04 PM by jvillarreal »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add Data Link through lisp
« Reply #1 on: December 07, 2011, 11:05:27 AM »
The 310 group is a "string representing hex value of binary chunk" (of data), so in your case it might convert (assuming 2 char width hex values) to a list of ints (219 7 12 0 2 0 6 0 13 0 40 0 54 0 0 0) which converted to a string is "Û\007\014\000\002\000\006\000\r\000(\0006\000\000\000". I would play with vaiations of data until a pattern emerges and suggests what the data, and the order of particular packets within the data chunk represents.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #2 on: December 07, 2011, 11:11:40 AM »
The 310 group is a "string representing hex value of binary chunk" (of data), so in your case it might convert (assuming 2 char width hex values) to a list of ints (219 7 12 0 2 0 6 0 13 0 40 0 54 0 0 0) which converted to a string is "Û\007\014\000\002\000\006\000\r\000(\0006\000\000\000". I would play with vaiations of data until a pattern emerges and suggests what the data, and the order of particular packets within the data chunk represents.

Thanks MP!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add Data Link through lisp
« Reply #3 on: December 07, 2011, 11:16:30 AM »
You're welcome JV. PS: I highly doubt the data is meant to manisfest as a string, I was just showing you what it looks like in different forms; kinda misleading on my part. It's binary data, i.e., what a data file might look like on disk, byte by byte. Sorry if I lead astray.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #4 on: December 07, 2011, 11:30:40 AM »
No worries. I wasn't even sure they were hex values.
There isn't much Data Link info on the site so any suggestion helps.

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #5 on: December 07, 2011, 03:39:08 PM »
Had some time to test after lunch...
After testing on several different csv and xls files, the binary chunk in group 310 seems to be utilized during the creation of the bitmap image for the preview..If the hex was left in the code, the data link was created but AutoCad would crash once it was selected in the manager(, while generating the preview).
Cancelling out that bit of code
Code: [Select]
  ;(cons 310 "DB070C00020006000D00280036000000")
seems to create a data link without any problems.

Lee Mac

  • Seagull
  • Posts: 12940
  • London, England
Re: Add Data Link through lisp
« Reply #6 on: December 07, 2011, 03:45:05 PM »
See DXF 310 here to confirm your suspicions.  :-)

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #7 on: December 07, 2011, 03:53:45 PM »
Thanks Lee. It would've been nice if the help files included a page for DATALINK group codes.
I'm sure I included plenty of other unecessary dxf groups as well.

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #8 on: December 07, 2011, 04:07:29 PM »
As it's shown in the help file for BLOCK_RECORD group codes as optional, I'd still be interested if anybody could explain how to generate the DXF binary chunk/hex string.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add Data Link through lisp
« Reply #9 on: December 07, 2011, 04:11:45 PM »
... I'd still be interested if anybody could explain how to generate the DXF binary chunk/hex string.

lol. I deleted the following because I thought it superfluous given the way the discussion went, but in the remote chance it helps "explain how to generate the DXF binary chunk/hex string":

Code: [Select]
(progn

    ;; Quick and dirty info/code, for what it's worth ...

    (defun _Num2Hex ( num / chars hex )

        (setq chars
           '(   "0" "1" "2" "3" "4" "5" "6" "7"
                "8" "9" "A" "B" "C" "D" "E" "F"
            )
        )

        (while (< 0 num)
            (setq
                hex (cons (nth (fix (rem num 16)) chars) hex)
                num (fix (/ num 16))
            )
        )

        (if hex (apply 'strcat hex) "0")

    )

    (defun _Hex2Num ( hex / mult sum )

        (setq
            mult 1.0
            sum  0.0
        )

        (foreach x

            (reverse
                (vl-string->list
                    (vl-string-translate ;; Thanks to Elpanov Evgeniy for
                        "ABCDEF"         ;; this clever snip to convert
                        ":;<=>?"         ;; resultant codes 65-70 to 58-63.
                        (strcase hex)    ;; Faster than subst, if clauses etc.
                    )
                )
            )

            (setq
                sum  (+ sum (* mult (- x 48)))
                mult (* mult 16.0)
            )

        )

        (fix sum)

    )

    (defun _Decode_Binary_Chunk ( hex_data / i result )

        (setq i (1+ (strlen hex_data)))

        (while (< 0 (setq i (- i 2)))
            (setq result
                (cons
                    (_Hex2Num (substr hex_data i 2))
                    result
                )
            )
        )

        result

    )

    (defun _Encode_Binary_Chunk ( bytes )

        (apply 'strcat
            (mapcar
                (function
                    (lambda ( byte / hex )
                        (if (eq 1 (strlen (setq hex (_Num2Hex byte))))
                            (strcat "0" hex)
                            hex
                        )
                    )
                )
                bytes
            )
        )

    )

    (defun _SimpleTest1 ( / data bytes )

        (setq
            data  "DB070C00020006000D00280036000000"
            bytes (_Decode_Binary_Chunk data)
        )

        (princ
            (strcat
                "\nHex Chunk converted to bytes:\n\n"
                (vl-prin1-to-string data)
                " >>> "
                (vl-princ-to-string bytes)
                "\n"
            )
        )

        (princ)

    )

    (defun _SimpleTest2 ( / bytes data )

        (setq
            bytes '(219 7 12 0 2 0 6 0 13 0 40 0 54 0 0 0)
            data   (_Encode_Binary_Chunk bytes)
        )

        (princ
            (strcat
                "\nBytes converted to Hex Chunk:\n\n"
                (vl-princ-to-string bytes)
                " >>> "
                (vl-prin1-to-string data)
                "\n"
            )
        )

        (princ)

    )

    (_SimpleTest1)

    (_SimpleTest2)

)

Output:

Hex Chunk converted to bytes:

      "DB070C00020006000D00280036000000" >>> (219 7 12 0 2 0 6 0 13 0 40 0 54 0 0 0)

Bytes converted to Hex Chunk:

      (219 7 12 0 2 0 6 0 13 0 40 0 54 0 0 0) >>> "DB070C00020006000D00280036000000"
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #10 on: December 07, 2011, 04:25:16 PM »
lol. I deleted the following because I thought it superfluous given the way the discussion went, but in the remote chance it helps "explain how to generate the DXF binary chunk/hex string":

Thanks :-)
I know the odds of someone explaining the hex generation are slim to none but that code will definitely come in handy.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add Data Link through lisp
« Reply #11 on: December 07, 2011, 04:48:25 PM »
I know the odds of someone explaining the hex generation are slim to none but that code will definitely come in handy.

?? Not sure I understand what you mean by 'explain the hex generation'.

An aside, converting a file's binary data to hex per this thread can be done by exploiting this ReadStream function (this isn't the most efficient route, but said code is already written) as well as other sample code in this thread:

(defun _FileToHex ( file / stream )
    (if (setq stream (_ReadStream file nil))
        (_Encode_Binary_Chunk
            (vl-string->list stream)
        )
    )   
)


However, when testing, never, ever do this:

(_FileToHex "c:/docs/some_file.xls")

It will take a long time and then your screen will be deluged with a monsoon of data.

Rather, do something like this:

(progn
    (setq hex_data (_FileToHex "c:/docs/some_file.xls"))
    (princ)
)


Afterwards query the var:

(substr hex_data 1 16) >>> "D0CF11E0A1B11AE1"

Finally, if adding it to dxf group data via the 310 group keep in mind you will have to do it in string 'packets', each a maximum of 254 chars (representing 127 bytes), i.e.

...
310 . "D0CF11E0A1B11AE1..."
310 . "A1B11D0CF11E0AE1..."
310 . "0D0CF1AEA1B1111E..."
...


Verbosely yours, mp.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jvillarreal

  • Bull Frog
  • Posts: 332
Re: Add Data Link through lisp
« Reply #12 on: December 07, 2011, 05:25:18 PM »
?? Not sure I understand what you mean by 'explain the hex generation'....
converting a file's binary data to hex..
I wanted to know how to generate the hex string in group 310 from the excel file and how its used to create the preview.
Thanks for the link to your ReadStream function and the explanation.
 :-o I have a lot of studying to do.

Thanks again!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add Data Link through lisp
« Reply #13 on: December 07, 2011, 05:30:15 PM »
You're most welcome. Sorry, I don't know how to yank "the preview data" from an excel file. However, once you have your data this might help ...

Splitting hex data into 254 char packets for dxf data representation ...

Code: [Select]
(defun _SplitByLen ( string len / i ubound result )

    ;; Split a string into a list of strings of size 'len'.

    (setq i (1+ (- len)) ubound (strlen string))

    (while (< (setq i (+ i len)) ubound)
        (setq result
            (cons
                (substr string i len)
                result
            )
        )
    )

    (reverse result)

)

Demo code:

Code: [Select]
(progn

    (mapcar 'print
        (mapcar
            (function (lambda (packet) (cons 310 packet)))
            ;; Not using a packet size of 254 in
            ;; the interests of posting clarity.
            (_SplitByLen
                ;; Only use a small subset of the stream
                ;; data for the demo, making sure length
                ;; is NOT a multiple of the packet size.
                (substr hex_data 1 640)
                48
            )
        )
    )
   
    (princ)
   
)

Output:

      (310 . "D0CF11E0A1B11AE100000000000000000000000000000000")
      (310 . "3E000300FEFF090006000000000000000000000010000000")
      (310 . "EB0700000000000000100000FEFFFFFF00000000FEFFFFFF")
      (310 . "00000000DB070000DC070000DD070000DE070000DF070000")
      (310 . "E0070000E1070000E2070000E3070000E4070000E5070000")
      (310 . "E6070000E7070000E8070000E9070000EA070000FFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
      (310 . "FFFFFFFFFFFFFFFF")
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

DeeGeeCees_V.2.0

  • Newt
  • Posts: 156
Re: Add Data Link through lisp
« Reply #14 on: November 21, 2023, 07:21:13 AM »
1. My apologies for digging up an old thread.
2. I'm trying to update an existing datalink with a new location and range.
3. I stumbled on this and it's close to what I think I need.
4. Is there a way to attach the new data created in AddDatalink.lsp to an existing table cell?

What I've got so far modifies an existing datalink, but I'm not sure if that's going to work. I'm not new to LISP but this stuff is a little over my head at the moment and I'm a little rusty.

Just looking for a nudge in the right direction... and some oil. :)