Help please.

Help please.

Anonymous
Not applicable
1,824 Views
19 Replies
Message 1 of 20

Help please.

Anonymous
Not applicable

I came across a situation today and would like to know if someone who dominates Lisp, could help me because I am very beginner.
is as follows, I have a .TXT file with name and location as shown in the image,
and I have a simple block with a text,

I would like to know how to insert this block by replacing the text of the block with the text described in the .txt with the coordinates of the txt.


Sorry for the English, google does not translate correctly.

 

[83278c12c51a06a1dace7be298377f0b]_Image%202018-01-25%20at%2010.57.00%20AM.png[89b1cb8e01b038a1248bad9045300b2a]_Image%202018-01-25%20at%2011.08.05%20AM.png

0 Likes
Accepted solutions (1)
1,825 Views
19 Replies
Replies (19)
Message 2 of 20

Kent1Cooper
Consultant
Consultant

If you redefine the Blocks to use ATTRIBUTES instead of Text/Mtext, a routine could easily be made to Insert them and assign the coordinates text string to the Attribute.

Kent Cooper, AIA
Message 3 of 20

Anonymous
Not applicable

@Kent1Cooper

 

Is there only such a possibility?
if so, it would have as to blast it to continue the text (example: 12/300)

0 Likes
Message 4 of 20

Kent1Cooper
Consultant
Consultant

A routine could be written to read a line from the text file, find where the space is in it, use the part before the space for the Block name, and use the part after for the insertion point and the Attribute content, if that sounds like what you want to do.

Kent Cooper, AIA
Message 5 of 20

Anonymous
Not applicable

It would be exactly that !!, lol
How would I write this? can you help me ?

0 Likes
Message 6 of 20

Kent1Cooper
Consultant
Consultant

I can't today -- maybe later.  It would involve at least the following AutoLisp functions that you could read up on:

 

(open) ; to get into the text file

(read-line) ; to pull a line of text from it

(vl-string-search) or (vl-string-position) ; to find where the space occurs

(substr) ; to extract a portion of it [for both portions]

(command) or (vl-cmdf) ; to Insert the Block and assign the Attribute value

Kent Cooper, AIA
Message 7 of 20

devitg
Advisor
Advisor

I want to follow it 

0 Likes
Message 8 of 20

Anonymous
Not applicable

follow him

0 Likes
Message 9 of 20

john.uhden
Mentor
Mentor

Sadly, or realistically, no one here dominates lisp, but there are many of us who keep trying.

In english, "dominates" means to rule over, like a king or emperor.  We here try only to win more battles than we lose.

 

Questions:

1.  Do you really want to insert a block at the coordinates, or will just text do?

    If text will do, skip to #6

2.  Is it the same block for each insertion?

3.  Does the block contain just one attribute?

4.  Is the block definition always in the current drawing?

5.  If not, is the block's DWG file found in AutoCAD's search path?

6.  Is the .TXT file space-delimited, meaning there is a space between the "name" and the coordinate?

7.  Is it always the same .TXT file (perhaps in the drawing's directory) or do you want to select it from somewhere in your computer or network?

8.  Might the .TXT file have the same name as the current drawing (except for the extension)?

9.  Is there anything else in the .TXT file that is contrary to the format you showed?

10.  Do you believe in the tooth fairy?

11.  Do you believe in telekinesis?  If so, please raise my hand.

John F. Uhden

Message 10 of 20

Anonymous
Not applicable

Hi @john.uhden
how do you know my english is bad and google does not translate correctly

1: Yes, the Blocks must be inserted in the coordinates.
2: Yes!
3: Yes!
4: No!
5: C:/FTTH_CAD/Blocos/poste9_3
6: Yes, there is only 1 space between the name and the coordinate.
7: I want to select it from somewhere.
8: Yes you can.
9: No!
10: lol, of course
11: lol

0 Likes
Message 11 of 20

john.uhden
Mentor
Mentor

I didn't say your english was bad.  You had apologized for it, and I was just having fun with the interpretation of "dominate."

In fact, your english is coming out quite well I think.

 

When I find the time, I can put it together for you, but I would bet that some kind person here will beat me to it.  But that's good for you.

I am glad to see you have a sense of humor.

John F. Uhden

Message 12 of 20

DannyNL
Advisor
Advisor
Accepted solution

Well, I did have some spare time and put something together specifically for your request.

 

Try the code below.

Make sure the block is inside the drawing or the path is added to your support paths.

 

(defun c:Test (/ T_BlockName T_VarList T_CurValues T_DataFile T_DataFile T_DataLine T_DataFields)
   (setq T_BlockName "POSTE9_3")
   (setq T_VarList '("CMDECHO" "OSMODE" "ATTREQ" "ATTDIA"))
   (setq T_CurValues (mapcar 'getvar T_VarList))
   (mapcar 'setvar T_VarList '(0 0 1 0))
   (if
      (and
         (or
            (tblsearch "BLOCK" T_BlockName)
            (setq T_BlockName (findfile (strcat T_BlockName ".dwg")))
         )
         (setq T_DataFile (getfiled "Select data file" "" "txt" 128))
      )
      (progn
         (vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-acad-object)))
         (setq T_DataFileID (open T_DataFile "r"))
         (while
            (setq T_DataLine (read-line T_DataFileID))
            (if
               (= (length (setq T_DataFields (String2List T_DataLine " "))) 2)
               (vl-cmdf "_.INSERT" T_BlockName (nth 1 T_DataFields) "" "" "" (nth 0 T_DataFields))
            )
         )
         (close T_DataFileID)
         (vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-acad-object)))
      )
      (alert " ** Block not found or no data file selected!")
   )
   (mapcar 'setvar T_VarList T_CurValues)
   (princ)
)
     
(defun String2List (S2L_String S2L_Delimiter / S2L_Return S2L_End S2L_EndPos S2L_Item)
  (setq S2L_End nil)
  (while
     (and
        (setq S2L_EndPos (vl-string-search S2L_Delimiter S2L_String))
        (not S2L_End)
     )
     (if
        S2L_EndPos
        (progn
	   (setq S2L_Item (substr S2L_String 1 S2L_EndPos))
           (if
              (/= S2L_Item "")
              (setq S2L_Return (append S2L_Return (list S2L_Item)))
           )           
	   (setq S2L_String (substr S2L_String (+ S2L_EndPos 2)))
        )
        (setq S2L_End T)
     )   
  )
  (if
     (/= S2L_String "")
     (setq S2L_Return (append S2L_Return (list S2L_String)))
  )
  S2L_Return  
)

 

Message 13 of 20

devitg
Advisor
Advisor

Hi Danny, please clear it 

 

 

(vl-cmdf "_.INSERT" T_BlockName (nth 1 T_DataFields) "" "" "" (nth 0 T_DataFields))
            )

The last argument shall be the rotation , but you sent the text from the line's first element.

 

And at last but not least , such block do no have attributte . 

 

3.  Does the block contain just one attribute? 

No, there is no attribute, it is a Block , just a rectangle with it´s diagonals, and a MTEXT beside the block reference

 

 

Command: LIST

Select objects: all
2 found

Select objects:

                  MTEXT     Layer: "Texto poste"
                            Space: Model space
                   Color: 7 (white)    Linetype: "BYLAYER"
                   Handle = 21e
             Style = "ROMANS"
        Annotative: No
Location:        X=   1.8795  Y=   0.7331  Z=   0.0000
Width:             12.4859
Normal:          X=   0.0000  Y=   0.0000  Z=   1.0000
Rotation:                0
Text height:        1.1543
Line spacing:    Multiple (1x =    1.9237)
Attachment:      TopLeft
Flow direction:  ByStyle
Contents:        \A1;9/300

                  BLOCK REFERENCE  Layer: "Poste"
                            Space: Model space
                   Color: 3 (green)    Linetype: "BYLAYER"
                   Handle = 21d
Message 14 of 20

Kent1Cooper
Consultant
Consultant

@devitg wrote:

.... 

And at last but not least , such block do no have attributte . 

 

3.  Does the block contain just one attribute? 

No, there is no attribute, it is a Block , just a rectangle with it´s diagonals, and a MTEXT beside the block reference

I'm not so sure....

 

In Post 1, the OP said:

... and I have a simple block with a text,

I would like to know how to insert this block by replacing the text of the block with the text described in the .txt with the coordinates of the txt.

 

I took that to mean that the posted entire drawing  [including a nested Block and a piece of Text] was to be inserted, becoming a Block in the target drawing.  That would mean each insertion would have that same Text in it, which is why I suggested an Attribute instead.  They said that's what they wanted, and answered Yes to @john.uhden's question about whether it would have just one Attribute.

 

@Anonymous, can we get a clarification?  Are you talking about Inserting the no-Attribute simple Block in your posted drawing, and adding Text separately next to it?  Or do you want the Block to include an Attribute for the text content?  Either way is possible, but the Attribute approach is easier, because there's no need to calculate a relative location for the Text [assuming the Attribute is positioned in the Block definition to account for whatever length its contents might be].

 

Kent Cooper, AIA
Message 15 of 20

DannyNL
Advisor
Advisor
Hi @devitg


@devitg wrote:

Hi Danny, please clear it 

 

 

(vl-cmdf "_.INSERT" T_BlockName (nth 1 T_DataFields) "" "" "" (nth 0 T_DataFields))
            )

The last argument shall be the rotation , but you sent the text from the line's first element.

 

And at last but not least , such block do no have attributte . 

 

3.  Does the block contain just one attribute? 

No, there is no attribute, it is a Block , just a rectangle with it´s diagonals, and a MTEXT beside the block reference

To be honest, I've not tested with the drawing as attached in the OP, but have created my own block similar to the image.

And in my case the syntax as provided works as expected to insert the block and fill the attribute, but I admit it depends on how the block is setup. This probably could be made more robust and foolproof.

 

And as Kent already noticed; John asked a list of questions and in post #10 it was confirmed it would be a block with an attribute.

Message 16 of 20

Anonymous
Not applicable

@DannyNL

Thank you, it worked perfectly, that was exactly the purpose.
You are very good.

 

0 Likes
Message 17 of 20

Anonymous
Not applicable

@Kent1Cooper

really, the initial goal was to be a simple block with a text, but it does not influence much, to be a block with attribute.

the lisp, which @DannyNL sent finalizes perfectly for the purpose,
Thank you for your help !!!

0 Likes
Message 18 of 20

devitg
Advisor
Advisor

Despite what it has been said. I check the OP original DWG , as post #1 , and the block has no attribute, maybe the OP has add it , and do not show.

 

Command: ATTEDIT

Select block reference:  That block has no editable attributes.
Select block reference:

Or I did no see it.

 

Hope OP clear it. 

0 Likes
Message 19 of 20

DannyNL
Advisor
Advisor

Good to hear & glad I could help Smiley Happy

0 Likes
Message 20 of 20

john.uhden
Mentor
Mentor

See.  I told you that others (smarter than I) would join in to help you.

You are very fortunate.

John F. Uhden

0 Likes