Jump to content

Please Help! I tried... (LeeMac)


ILoveMadoka

Recommended Posts

Lee (or anyone else)

 

I tried to modify the code you wrote for me to create a view based upon a selected text string.

I am trying to create a block in the exact same way.

Some of it works but I'm not prompted for the base point and the block is not created.

Here is my failed attempt..

 


;; https://www.cadtutor.net/forum/topic/66487-getstring-pasteclip-question/
;; Modified Code originally written by Lee Mac 12/07/2018
;; Selected text MUST be TEXT
;; =========================

(defun c:BN0 ( / cmd ent pt1 ss0 str )
    (while
        (not
            (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect text desired block name name: ")))
                (cond
                    (   (= 7 (getvar 'errno))
                        (prompt "\nMissed, try again.")
                    )
                    (   (null ent))
                    (   (/= "TEXT" (cdr (assoc 0 (entget ent))))
                        (prompt "\nThe selected object is not single-line text.")
                    )
                    (   (= "" (setq str (cleanstring (cdr (assoc 1 (entget ent))))))
                        (prompt "\nThe text content is not valid for use as a valid name.")
                    )
                    (   
                        (princ "Select objects for Block...\n")
                        (setq ss0 (ssget))
                    )
                    (   (and (setq pt1 (getpoint "\nSelect Base Point for Block: "))
                        )
                        (setq cmd (getvar 'cmdecho))
                        (setvar 'cmdecho 0)
                        (vl-cmdf "_.-block" str pt1 ss0 " ")
                        (setvar 'cmdecho cmd)
                    )
                )
            )
        )
    )
    (princ)
)
(defun cleanstring ( str )
    (while (wcmatch str "*%%[OoUu]*")
        (foreach c '("O""o""U""u") (setq str (vl-string-subst "" (strcat "%%" c) str)))
    )
    (vl-string-trim " " (vl-string-translate "\\<>/?\":;*|,=`" "               " str))
)
(princ)



;;; ======================================================================================

 

Link to comment
Share on other sites

Happy with this?

Command O2B , as in objects 2 block

 


(defun cleanstring ( str )
    (while (wcmatch str "*%%[OoUu]*")
        (foreach c '("O""o""U""u") (setq str (vl-string-subst "" (strcat "%%" c) str)))
    )
    (vl-string-trim " " (vl-string-translate "\\<>/?\":;*|,=`" "               " str))
)

;; based on https://www.cadtutor.net/forum/topic/6086-convert-to-block/?page=0
;; I put all user input outside of this function
(defun obj2blk1 (ss bn pt / i ent elist)

  ;;; Create BLOCK Header
   (entmake (list (cons 0 "BLOCK") (cons 10 pt) (cons 2 bn) (cons 70 0)))

  ;;;STEP THRU THE SET
   (setq i (sslength ss))
   (while (>= i (setq i (1- i)) 0)
       (setq ent   (ssname ss i)
             elist (entget ent)
       ) ;_  end setq
       (entmake elist)
   ) ;_  end while

  ;;;FINISH THE BLOCK DEFINITION
   (entmake (list (cons 0 "ENDBLK") (cons 8 "0")))

  ;;;Insert the Block & Delete Originals
   (entmake (list (cons 0 "INSERT") (cons 2 bn) (cons 8 "0") (cons 10 pt)))
   (command "_.ERASE" ss "")
   (redraw)
   (prin1)
) ;_  end defun

(defun c:o2b (/ ent ss bn pt i ent elist)
  ; Get Block Name
  ;; (setq bn (getstring "\nSpecify Block Name: "))
 
  (setq ent (car (entsel "\nSelect text desired block name name: ")))
  (if (= "TEXT" (cdr (assoc 0 (entget ent))))
    (setq bn (cleanstring (cdr (assoc 1 (entget ent)))))
  )
 
  ; Get Entities
  (princ "\nSelect Objects to Convert to Blocks:")
  (setq ss (ssget '((-4 . "<NOT") (0 . "INSERT,POLYLINE,VIEWPORT") (-4 . "NOT>"))))
 
  ; Get Base Point
  (initget 1)
  (setq pt (getpoint "\nSpecify Base Point for Block: "))

  ;;
  (obj2blk1 ss bn pt)

)


Link to comment
Share on other sites

Spotted the mistake in the code real simple worked for me a double "" is equal to pressing enter or in most cases exit a command.

(vl-cmdf "_.-block" str pt1 ss0 " ")
(vl-cmdf "_.-block" str pt1 ss0 "")
Edited by BIGAL
Link to comment
Share on other sites

The issue with your modification to the code lies with these expressions:

 

23 hours ago, ILoveMadoka said:

                    (   
                        (princ "Select objects for Block...\n")
                        (setq ss0 (ssget))
                    )
                    (   (and (setq pt1 (getpoint "\nSelect Base Point for Block: "))
                        )
                        ...
                    )

 

 

Here, the princ expression is constituting the test expression for the cond statement and, since princ returns a non-nil value, the test expression is validated, causing the ssget expression to be evaluated, and the cond function then ceases evaluation since one of the conditions has been validated.

 

You could write these expressions in the following way:

                    (   (and (setq ss0 (ssget))
                             (setq pt1 (getpoint "\nSelect Base Point for Block: "))
                        )
                        (setq cmd (getvar 'cmdecho))
                        (setvar 'cmdecho 0)
                        (vl-cmdf "_.-block" str pt1 ss0 "")
                        (setvar 'cmdecho cmd)
                    )

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...