Jump to content

Recommended Posts

Posted

Hi everyone,

I wrote an AutoLISP program that extracts a DWG’s contents into the current drawing without opening the file. Since VLA_OBJECTs are limited, I used entget, but AutoCAD crashes with a Fatal Error.

Does anyone know what might be wrong or which part of the code I should fix?
 

(defun MT:GetFileD (title lastPath defaultfile ext flag /)
 (or (= 'STR (type defaultfile)) (setq defaultfile ""))
 (if (= 'STR (type lastPath))
  (setq defaultfile (strcat lastPath "\\" defaultfile))
 )
 (getfiled title defaultfile ext flag)
)

(defun MT:GetEntgetListFromDwg (dwgfilename / doc ss entgetlist)
 (if (setq dwgfilename (findfile dwgfilename))
  (progn
   (setq doc (vla-open (vla-get-Documents (vlax-get-acad-object)) dwgfilename :vlax-false))
   (setq entgetlist '())
   (setq ss (vla-get-ModelSpace doc))
   (vlax-for obj ss
    (setq entgetlist (cons (entget (vlax-vla-object->ename obj)) entgetlist))
   )
   (vlax-release-object ss)
   (setq ss nil)
   (vla-close doc)
   (vlax-release-object doc)
  )
 )
 entgetlist
)

(defun MT:RefineEntgetList (entgetlist / item)
 (foreach item entgetlist
  (setq newitem (vl-remove (assoc -1 item) item))
  (setq newitem (vl-remove (assoc 330 item) newitem))
  (setq newitem (vl-remove (assoc 5 item) newitem))
  (setq entgetlist (subst newitem item entgetlist))
 )
 entgetlist
)

;;;(c:CompareDwgs)
(defun c:CompareDwgs (/ regKey lastPath mainfile revisedfile entlist-main entlist-revised entlist-first entlist-second
                      entlist-both ss ent
                     )
 ;; registry key to store last path
 (setq regKey "HKEY_CURRENT_USER\\Software\\Ahankhah\\CompareDwgs")
 (setq lastPath (vl-registry-read regKey "LastPath"))
 ;; ask user for first file
 (setq mainfile (MT:GetFileD "Select main drawing" lastPath "main.dwg" "dwg" 0))
 ;; if user cancelled, exit gracefully
 (if (not mainfile)
  (progn (princ "\nOperation canceled by user.") (princ))
  ;; else proceed to ask for second file
  (progn
   (setq lastPath (vl-filename-directory mainfile))
   (vl-registry-write regKey "LastPath" lastPath)
   ;; use folder of first file as default for second
   (setq revisedfile (MT:GetFileD "Select revised drawing" lastPath "revised.dwg" "dwg" 0))
   ;; if user cancelled selecting second file, exit gracefully
   (if (not revisedfile)
    (progn (princ "\nOperation canceled by user.") (princ))
    ;; else we have both files — save last path (folder of second) and continue
    (progn
     (setq lastPath (vl-filename-directory revisedfile))
     (vl-registry-write regKey "LastPath" lastPath)
     (setq entlist-main (MT:GetEntgetListFromDwg mainfile))
     (setq entlist-revised (MT:GetEntgetListFromDwg revisedfile))
     (setq entlist-first (MT:RefineEntgetList entlist-main))
     (setq entlist-second (MT:RefineEntgetList entlist-revised))
     (setq entlist-both '())
     (foreach elist entlist-first
      (if (member elist entlist-second)
       (progn
        (setq entlist-first (vl-remove elist entlist-first))
        (setq entlist-second (vl-remove elist entlist-second))
        (setq entlist-both (cons elist entlist-both))
       )
      )
     )
     (princ "\nComparison done. Lists are ready.")
     (foreach elist entlist-both (entmakex elist))
     (foreach elist entlist-first
      (if (assoc 62 elist)
       (setq elist (subst (cons 62 1) (assoc 62 elist) elist))
       (setq elist (append elist (list (cons 62 1))))
      )
      (entmakex elist)
     )
     (foreach elist entlist-second
      (if (assoc 62 elist)
       (setq elist (subst (cons 62 2) (assoc 62 elist) elist))
       (setq elist (append elist (list (cons 62 2))))
      )
      (entmakex elist)
     )
     (princ)
    )                                             ; end else have revisedfile
   )                                              ; end if revisedfile
  )                                               ; end else have mainfile
 )                                                ; end if mainfile
)

 

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