Lisp to paste contents of clipboard with specified font and spacing etc

Lisp to paste contents of clipboard with specified font and spacing etc

paul9ZMBV
Advocate Advocate
1,412 Views
11 Replies
Message 1 of 12

Lisp to paste contents of clipboard with specified font and spacing etc

paul9ZMBV
Advocate
Advocate

Hi

 

Could someone kindly offer a lisp to do the following:

 

What I'm currently having to do is copy text from an excel sheet and past in AutoCad using "paste,special,text"

While this works I then have to then change the following parameters,

 

  • font=Arial
  • text height=2.2171
  • line space factor=0.8727
  • line spacing distance=3.2249 
  • line space style=exactly
  • justify=top left
  • defined width=95
  • defined height=270

It would be great to have a lisp command that pastes the text in clipboard with the above parameters, no need for user input as the parameters above are always constant

 

Any help much appreciated

 

 

 

0 Likes
Accepted solutions (1)
1,413 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

When I Paste something copied out of Excel, it simply comes in as Mtext in the current Style [with whatever font is in its definition] and height, not using the font or size from the Excel file, so all that should be needed for the font is to set the Text Style you want.  Or is it your intent that the Mtext should have internal formatting forcing Arial font on the entire content, regardless of the nominal text Style?

[The line spacing distance is not needed, because it is not stored in entity data, but is only a result of the text height and the line space factor.]

As for "defined height," there should be no need to specify that unless Columns are involved, in which case there's an entry for "Column Autoheight."  Is that what you mean?

Are you thinking text height applied in a Style that has zero defined height, or using a Style that has your desired height fixed in its definition?

Apart from these questions, the rest should not be difficult to impose with AutoLisp (append)/(entmod) functions on the latest object after a text Style setting and a PASTECLIP.

Kent Cooper, AIA
0 Likes
Message 3 of 12

paul9ZMBV
Advocate
Advocate

Thanks Kent

Or is it your intent that the Mtext should have internal formatting forcing Arial font on the entire content, regardless of the nominal text Style?

Correct it would be good that the Lisp forced this and then set it back to whatever it was on

 

Column Autoheight would be good

 

The mtext column width is important at 95 as it has to fit exactly each time

 

ariel with a defined height of 2.2171 is required

 

can spacing not be defined at 3.2249 ?

 

0 Likes
Message 4 of 12

Kent1Cooper
Consultant
Consultant

@paul9ZMBV wrote:

.... can spacing not be defined at 3.2249 ?


It can be, using:

(vla-put-LineSpacingDistance VLA-ObjectName 3.2249)

with conversion of the Mtext to a VLA object, or without the need for that,

(setpropertyvalue EntityName "LineSpaceDistance" 3.2249)

but not through entity data, because it contains no entry for that, but it's only a result of the relationship between the text height and the line spacing factor.

The advantage of the entity data approach is that you can impose all of these things at once, with a big (append) of desired-value entries at the end, which will override earlier existing entries for the same properties in the entity data list.  With the above approaches, it's necessary to handle each property with its own separate (vla-put-) or (setpropertyvalue) function.

 

And the line spacing distance and line spacing factor are locked to each other -- change one, and it changes the other -- so there's no point in forcing both.  So I would do the factor, as part of a collective entity data modification, and the distance will follow.

Kent Cooper, AIA
0 Likes
Message 5 of 12

paul9ZMBV
Advocate
Advocate

sound good

0 Likes
Message 6 of 12

Kent1Cooper
Consultant
Consultant

@paul9ZMBV wrote:

....

Column Autoheight would be good

....


Would you ever be bringing in enough content in one Pasting to involve Columns?  If so, there are other Column-related choices to be made.  But that sounds like a lot from one Excel cell.  If I Copy/Paste more than one cell into an AutoCAD drawing, or just a cell without getting inside it to Copy out the text content, I get an OLE object, not Mtext that can have all those properties assigned.

 

Maybe an example of a [small] Excel file and a drawing file with the desired result would help us understand.

Kent Cooper, AIA
0 Likes
Message 7 of 12

paul9ZMBV
Advocate
Advocate

Hi

I'm automating specifications for joinery drawings within excel and the pasting it to a layout far right hand side

please examples attached, the excel sheet needs more developing but just want to make sure it works in AutoCad before progressing with it to much

 

Excel and required results in AutoCad attached. 

 

many thanks

0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

[Before Message 7 arrived]

I find that (append)ing a text-content entry on the end of the entity data list doesn't replace the earlier one, but appends content to it, resulting in a doubling of the content, the first being the original in whatever Style/font that is, and the second having the Arial font override.  So it's necessary to replace the entry wholesale [which could be done with a little less code using (setpropertyvalue) instead of what's below, but since we're into entity data for the other reason...].

And I find the code-79 entry for Column Autoheight rejected, probably because by default my Mtext has no columns, reinforcing the need to specify other Column-related settings if they're really needed [see my previous Reply].

Ignoring that, this seems to work in minimal testing:

 

(defun C:WHATEVER (/ edata)
  (command-s "_.pasteclip")
  (setq edata (entget (entlast)))
  (if (member '(0 . "MTEXT") edata); it's the right kind of thing
    (progn ; then
      (setq edata
        (subst ; replace:
          (cons 1 ; text content
            (strcat
              "{\\fArial|b0|i0|c0|p34;"
              (cdr (assoc 1 edata))
              "}"
            ); strcat
          ); cons
          (assoc 1 edata); original without font override
          edata
        ); subst
      ); setq
      (entmod
        (append
          edata
          '(
            (40 . 2.2171); height
            (44 . 0.8727); line space factor
            (73 . 2); line space style=exactly
            (71 . 1); justify=top left
            (41 . 95.0); defined width
          ); list
        ); append
      ); entmod
    ); progn
  ); if
  (prin1)
)

 

Your last Copy-stuff-to-the-clipboard needs to have been text content from inside a cell in Excel, or pulled from some Text/Mtext object in AutoCAD, or probably some other possible text sources, but not just anything.  For example, it doesn't work pulling content from MS Word, which Pastes in as an OLE object.

Kent Cooper, AIA
0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

@paul9ZMBV wrote:

....I'm automating specifications for joinery drawings within excel and the pasting it to a layout far right hand side ... examples attached,...


If you're talking about making the list of Notes in the drawing, as one Mtext object, my routine won't be able to deal with content from multiple cells at once.  And the cells whose content is concatenations of content from other cells are way out of the question.  I think some very different approach would be in order, but it might be that it needs to be within Excel, not AutoCAD, to merge all those cells, or something.

Kent Cooper, AIA
0 Likes
Message 10 of 12

paul9ZMBV
Advocate
Advocate

Hi Kent,

 

i understand, I've changed the spreadsheet to Concatenate into one cell, from that i can copy and paste into the cell next to it using paste values, i cant seem to find a way of doing that automatically via a formula into another cell from the Concatenate cell but that's not really and issue, just copying the one cell and pasting values into a another 

 

using this method your Lisp works great

 

It would be great if you could have the insertion point at the top so i can insert the copied text using the Lisp into the correct position by clicking the far left of the  horizontal line shown in the attached.

 

This would save even more time not having to position the text accurately each time, the insertion point would be above the actual box so the first line of text (NOTES) is down a little from the horizontal line as per the sample

 

re attached samples

 

thanks so much for all your help

 

thanks again

0 Likes
Message 11 of 12

Kent1Cooper
Consultant
Consultant

Well, there's a problem when the content gets that long.  It splits up the text-content entity data entries into a bunch of code-3 entries [I think 250 characters' worth?] and finally a code-1 entry with whatever's left less than that long.  So it can't just substitute it all in one code-1 entry replacement.  The (getpropertyvalue) approach keeps all the content in one piece, but the "Text" property there is read-only, so you can't replace it that way.

 

I'm not sure what can be done.  It might require breaking up the content into 250-character [if that's the right number] pieces for 3-code entries and a 1-code closer, if that's possible -- not a simple procedure, not knowing how many will be needed, etc.

 

I think the easiest way to move the content down a notch from the insertion point would be to begin it with a hard return or two.

Kent Cooper, AIA
0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

Is this posted elsewhere I am sure responded can read direct from excel so theoretically can make an identical table. Will look for other post. You can get all sorts of info about a Excel cell from CAD it is about finding the answer, normally its a VBA answer as that is what Excel macros use. Often the excel cell property is not the name you would think to look for. 

 

Found it, Lisp to paste contents of clipboard with specified font and spacing etc - AutoLISP, Visual LISP & DC...

 

Looking at your excel if you lay it out a bit more no need for concatenation,  if you use cells can read say cells and join the strings in CAD. You can read columns F G H I J K etc and strcat them together. 

 

For me I have started to read and write Excel & Libre Calc as a standard method. 

 

I just wonder if there is really a need at all for Excel to be used rather than making the notes from stored data inside the dwg, yes you can save stuff in a dwg that is not dwg objects. 

 

Your sample dwg only shows Notes, not objects that may have the Note info attached to them. Sometimes need to think outside the square.

 

 

 

0 Likes