Jump to content

Recommended Posts

Posted

Hi,

 

I have the following setup:

 

DWG called Test1 with a block called Block1

DWG called Test2 with a block called Block1

 

I try to insert Block1 from Test2 into the Test1.

The code here below assumes that it will ask to redefine, so I default to "Y" after entering the blockname. Which works fine.

(defun c:InsertContentTest()
  (setq filePath (getfiled "Select the DWG file" "./" "dwg" 4)) 
  (vl-cmdf "-INSERTCONTENT" filePath "Block1" "Y" "0,0" 1 1 0)
)

 

How can I make this dynamic, because in some cases (e.g. when the block has already been redefined) it doesn't ask to redefine anymore. In that case I just want to skip the "Y":

 

  (vl-cmdf "-INSERTCONTENT" filePath "Block1" "0,0" 1 1 0)


I tried e.g. LASTPROMPT to check whether it asks to redefine, but that doesn't really work. I tried to make it skip "Y" when an invalid input is given, but that didn't work.
 

Regards

Test1.dwg Test2.dwg

Posted

Another is check does it exist.

 

(if (tblsearch "block" bname)
  (vl-cmdf "-INSERTCONTENT" filePath "Block1" "Y" "0,0" 1 1 0)
  (vl-cmdf "-INSERTCONTENT" filePath "Block1" "0,0" 1 1 0)
)

 

  • Like 1
Posted

Hi, thank you both for the answers, but it still not working. Here is a specific problem:

When I run the insertcontenttest lisp, it correctly detects that there is already is a Block1 in Test1, so it asks whether I want to redefine. That's good.
 

-INSERTCONTENT External file path:C:\Working Folder\Productgroups\013 Jacky\Test2.dwg Block name or [?]Block1
Block "Block1" already exists. Redefine it? [Yes/No] <N>: Y
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: 0,0
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>: 1 Enter Y scale factor <use X scale factor>: 1
Specify rotation angle <0>: 0
Regenerating model.
Command:


But when I proceed to do it a second time, it doesn't ask to redefine anymore, even though there is a block called Block1 in Test1 drawing. Why?

 

Command: INSERTCONTENTTEST
-INSERTCONTENT External file path:C:\Working Folder\Productgroups\013 Jacky\Test2.dwg Block name or [?]Block1
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: Y Specify Y scale factor <1>: 0,0  Specify second point: 1
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: 1
Specify rotation angle <0>: 0
Command:


You can see above that it is not asking for redefine anymore.

@BIGAL If I then use that code to check if it exists, it said in both cases that it exists. But then that would imply that for both times it should ask to redefine. right? 

That's why I was wondering if there is a way to detect what the prompt is asking (asking redefine or not asking redefine) and input based on that.

 

Regards

 

Posted (edited)
(defun c:inscont ( / *error* filepath bname sd qa su at cm ex )

  (defun *error* ( m )
    (if sd
      (setvar (quote sdi) sd)
    )
    (if qa
      (setvar (quote qaflags) qa)
    )
    (if su
      (setvar (quote suppressalerts) su)
    )
    (if at
      (setvar (quote attdia) at)
    )
    (if cm
      (setvar (quote cmddia) cm)
    )
    (if ex
      (setvar (quote expert) ex)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq sd (getvar (quote sdi)))
  (setvar (quote sdi) 0)
  (setq qa (getvar (quote qaflags)))
  (setvar (quote qaflags) 4)
  (setq su (getvar (quote suppressalerts)))
  (setvar (quote suppressalerts) 1)
  (setq at (getvar (quote attdia)))
  (setvar (quote attdia) 0)
  (setq cm (getvar (quote cmddia)))
  (setvar (quote cmddia) 0)
  (setq ex (getvar (quote expert)))
  (setvar (quote expert) 2)
  (setq bname (getstring t "\nSpecify name of block to be redefined in another DWG file : "))
  (setq filepath (getfiled (strcat "Specify *.dwg file that contains : \"" bname "\" block for redefinition...") (getvar (quote dwgprefix)) "dwg" 0))
  (if (tblsearch "BLOCK" bname)
    (progn
      (vl-cmdf "_.-INSERTCONTENT" filepath bname)
      (while (< 0 (getvar (quote cmdactive)))
        (vl-cmdf)
      )
    )
  )
  (*error* nil)
)

 

Not sure as my BricsCAD don't have -INSERTCONTENT command, but I think that somehing among this code should help...

Edited by marko_ribar
Posted (edited)

Here is another one, but this time my AutoCAD 2022 crashes...

 

(defun c:inscont ( / *error* SwitchDWG startpath filepath bname sd ce qa su at cm ex ff fl )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (defun *error* ( m )
    (if sd
      (setvar (quote sdi) sd)
    )
    (if ce
      (setvar (quote cmdecho) ce)
    )
    (if qa
      (setvar (quote qaflags) qa)
    )
    (if su
      (setvar (quote suppressalerts) su)
    )
    (if at
      (setvar (quote attdia) at)
    )
    (if cm
      (setvar (quote cmddia) cm)
    )
    (if ex
      (setvar (quote expert) ex)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (defun SwitchDWG ( drawing )
    (vlax-for doc (vla-get-documents (vlax-get-acad-object))
      (if (= (vla-get-fullname doc) drawing)
        (vla-put-windowstate doc acmax)
        (vla-put-windowstate doc acmin)
      )
    )
  )

  (defun chkopen ( filepath / f )
    (vlax-for doc (vla-get-documents (vlax-get-acad-object))
      (if (= (vla-get-fullname doc) filepath)
        (setq f t)
      )
    )
    f
  )

  (setq sd (getvar (quote sdi)))
  (setvar (quote sdi) 0)
  (setq ce (getvar (quote cmdecho)))
  (setvar (quote cmdecho) 0)
  (setq qa (getvar (quote qaflags)))
  (setvar (quote qaflags) 4)
  (setq su (getvar (quote suppressalerts)))
  (setvar (quote suppressalerts) 1)
  (setq at (getvar (quote attdia)))
  (setvar (quote attdia) 0)
  (setq cm (getvar (quote cmddia)))
  (setvar (quote cmddia) 0)
  (setq ex (getvar (quote expert)))
  (setvar (quote expert) 0)
  (setq startpath (strcat (getvar (quote dwgprefix)) (getvar (quote dwgname))))
  (setq bname (getstring t "\nSpecify name of block to be redefined in another DWG file : "))
  (setq filepath (getfiled (strcat "Specify *.dwg file that contains : \"" bname "\" block for redefinition...") (getvar (quote dwgprefix)) "dwg" 0))
  (if (chkopen filepath)
    (progn
      (prompt (strcat "\nFile : \"" filepath "\" is opened... Close this DWG next time before you start this routine... Quitting..."))
      (exit)
    )
    (progn
      (if (tblsearch "BLOCK" bname)
        (setq ff t)
      )
      (SwitchDWG filepath)
      (if (tblsearch "BLOCK" bname)
        (setq fl t)
      )
      (SwitchDWG startpath)
      (if (and ff fl)
        (progn
          (vl-cmdf "_.-INSERTCONTENT" filepath bname)
          (while (< 0 (getvar (quote cmdactive)))
            (vl-cmdf)
          )
        )
      )
    )
  )
  (*error* nil)
)

 

HTH.

M.R.

Edited by marko_ribar
Posted

if the block has been redefined the first time why should AutoCad ask you again?

AutoCad / tblsearch can tell you if a block with a certain name is already present or not but after you insert it the question becomes pointless because now the block exists.

If you want to insert every time the block on file you would have to use something like this :

 

(defun c:iblk ( / dwg MySymName)
  (setq dwg "C:/MySymbols/MySymbol.dwg" MySymName "MySymbol")
  (if (tblsearch "block" MySymName)
    (setq MySymName (strcat MySymName "=" dwg))
    (setq MySymName dwg)
  )
  ;;; (command "-insert" MySymName etc...)
)

 

Posted (edited)

This will not crash CAD, but still I can't get rid of dialog message "Do you want to redefine block"... You even don't need to open *filepath* *.dwg as it is using ObjectDBX and checking of existance of "Block1" block is performed silently to both *.dwg files... So here is routine :

 

(defun c:inscont ( / *error* GetAxDbDoc filepath bname sd ce qa su at cm ex ff fl doc )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (defun *error* ( m )
    (if sd
      (setvar (quote sdi) sd)
    )
    (if ce
      (setvar (quote cmdecho) ce)
    )
    (if qa
      (setvar (quote qaflags) qa)
    )
    (if su
      (setvar (quote suppressalerts) su)
    )
    (if at
      (setvar (quote attdia) at)
    )
    (if cm
      (setvar (quote cmddia) cm)
    )
    (if ex
      (setvar (quote expert) ex)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (defun GetAxDbDoc ( filename / majVer progId axdbdoc )
    (setq progId
      (if (< (setq majVer (substr (getvar 'acadver) 1 2)) "16")
        "ObjectDBX.AxDbDocument"
        (strcat "ObjectDBX.AxDbDocument." majVer)
      )
    )
    (if (setq axdbdoc (vlax-create-object progId))
      (if (vl-catch-all-apply (function vla-open) (list axdbdoc filename))
        (not (vlax-release-object axdbdoc))
        axdbdoc
      )
    )
  )

  (setq sd (getvar (quote sdi)))
  (setvar (quote sdi) 0)
  (setq ce (getvar (quote cmdecho)))
  (setvar (quote cmdecho) 0)
  (setq qa (getvar (quote qaflags)))
  (setvar (quote qaflags) 4)
  (setq su (getvar (quote suppressalerts)))
  (setvar (quote suppressalerts) 1)
  (setq at (getvar (quote attdia)))
  (setvar (quote attdia) 0)
  (setq cm (getvar (quote cmddia)))
  (setvar (quote cmddia) 0)
  (setq ex (getvar (quote expert)))
  (setvar (quote expert) 2)
  (setq bname (getstring t "\nSpecify name of block to be redefined in another DWG file : "))
  (setq filepath (getfiled (strcat "Specify *.dwg file that contains : \"" bname "\" block for redefinition...") (getvar (quote dwgprefix)) "dwg" 0))
  (if (tblsearch "BLOCK" bname)
    (setq ff t)
  )
  (if (setq doc (GetAxDbDoc filepath))
    (progn
      (vlax-for blk (vla-get-blocks doc)
        (cond
          ( (and (vlax-property-available-p blk (quote effectivename)) (= (vla-get-effectivename blk) bname))
            (setq fl t)
          )
          ( (and (vlax-property-available-p blk (quote name)) (= (vla-get-name blk) bname))
            (setq fl t)
          )
        )
      )
      (vlax-release-object doc)
    )
  )
  (if (and ff fl)
    (progn
      (vl-cmdf "_.-INSERTCONTENT" filepath bname)
      (while (< 0 (getvar (quote cmdactive)))
        (vl-cmdf)
      )
    )
  )
  (*error* nil)
)

 

HTH.

M.R.

Edited by marko_ribar

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...