Jump to content

Creating lisp and DCL routine to load a template with chosen layout tab and delete other tabs


fase

Recommended Posts

Hi All,

 

I'm trying to create a lisp that will prompt me to select a template once selected it prompts the user to select the layout tab (paper space), after that's done it will create a new sheet with the template and delete the unused tabs. I am not very familiar with lisping so please bare with me however I have provided the lisp and DCL code below. 

 

Currently the DCL will popup and you can run through the prompts, after selecting the layout however nothing happens. Could anyone please assist me as this is rather urgent.

 

the lisp:

(defun c:CreateNewSheet ( / templatePath dcl)
  ; Select Template Path
  (setq templatePath (getfiled "Select Template" "" "dwt" 0)) 
  (if (not templatePath)
    (progn
      (princ "\nUser must select a path. ")
      (exit)
      )
    )

  ; Search for DCL File
  (setq dcl (findFile "CreateNewSheet.dcl"))
  (if (not dcl)
    (progn
      (princ "\nCould not find CreateNewSheet.dcl")
      (exit)
      )
    )

  ; Load the DCL File
  (setq dclId (load_dialog dcl))
  (if (not dclId)
    (progn
      (alert "Failed to load dialog box.")
      (exit)
      )
    )

  ; Attempt to Display Dialog
  (if (not (new_dialog  "createNewSheet" dclId))
    (progn
      (alert "Failed to display dialog box.")
      (unload_dialog dclId)
      (exit)
      )
    )

  ; Set default template path in the dialog
  (set_tile "template" templatePath)

  ; Example list of layout tabs, replace with actual list
  (setq layoutTabsList (list "SAP_A1_H" "SAP_A1_VER")) 
  (start_list "layoutTabs")
  (foreach tab layoutTabsList
    (add_list tab)
    )
  (end_list )

  (start_dialog)
  (unload_dialog  dclId) 
)


 ; Function to be called when OK button is pressed
(defun CreateSheetOK ()
  ; Get selected template path
  (setq selectedTemplate (get_tile "template"))

  ; Get selected layout tab
  (setq selectedLayout (get_tile "layoutTabs"))

  ; Create new drawing with selected layout
  (create_drawing selectedTemplate selectedLayout)

  ; Close dialog
  (done_dialog 1)
  )


; Function to be called when Cancel button is pressed
(defun CreateSheetCancel ()
  ; Close the dialog box
  (done_dialog 0)
  )

(defun create_drawing (templatePath layoutName)
  (setq acad (vlax-get-or-create-object "AutoCAD.Application"))
  (vlax-invoke-method acad 'Activate)
  (setq doc (vlax-invoke-method acad 'Documents 'Add templatePath))
  (setq layouts (vla-get-layouts doc))
  (vla-delete (vlax-invoke layouts 'Item layoutName))
)

 

The DCL:

// CreateNewSheet.dcl

createNewSheet : dialog {
label = "Create New Sheet";
: row {
: text { label = "Select Template:"; }
: edit_box { key = "template"; width = 30; }
: button { label = "Browse..."; }
}
: row {
: list_box { key = "layoutTabs"; width = 30; }
}
: row {
: button { key = "ok"; label = "OK"; is_default = true; action = "(CreateSheetOK)"; }
: button { key = "cancel"; label = "Cancel"; is_cancel = true; action = "(CreateSheetCancel)"; }
}
}

 

Link to comment
Share on other sites

i would write a dynamic dcl within the lisp.
take a look at the ChooseFileAndItem function.

(defun c:CreateNewSheet ( / chooseFileAndItem layoutTabsList layoutinfo)
  
  ;|
    creates a dcl to select a filepath + custom item
    @Param itemlist \<list> list of items to select
    @Returns \<list> '(filepath selected-item)
  |;
  (defun chooseFileAndItem (itemlist / write_dcl setpath actionstring i keys file dcl_id path ans)
    
    (defun write_dcl (keylist / fo fname); keylist = '(("key" . "label")("key2" . "label2")..)

      (setq fo (open (setq fname (vl-filename-mktemp "chooseTemplate" "" ".dcl")) "w"))
      
      (write-line "createNewSheet : dialog {" fo)
      (write-line "  label = \"Create New Sheet\";" fo)
      (write-line "  : row {" fo)
      (write-line "    : text { label = \"Select Template:\"; }" fo)
      (write-line "    : edit_box { key = \"template\"; width = 30; }" fo)
      (write-line "    : button { key = \"browse\"; label = \"Browse...\"; }" fo)
      (write-line "  }" fo)
      (write-line "" fo)

      (mapcar '(lambda (x) (write-line (strcat ": button {key = \"" (car x) "\"; label = \"" (cadr x) "\";}") fo)) keylist)

      (write-line "" fo)
      (write-line "  spacer_1;" fo)
      (write-line "  : button { key = \"cancel\"; label = \"Cancel\";" fo)
      (write-line "    fixed_width = true; alignment = right;" fo)
      (write-line "    is_cancel = true;" fo)
      (write-line "  }" fo)
      (write-line "}" fo)
      
      
      (close fo)
      fname
    )
    
    (defun setpath (path / newpath)
      (setq newpath (getfiled "Select Template" path "dwt" 0)) 
      (if newpath
        (set_tile "template" newpath)
        (set_tile "template" path)
      )
    )
    
    (setq i 0)
    (setq keys 
      (mapcar '(lambda (x) (list (strcat "t" (itoa (setq i (1+ i)))) x)) itemlist)
    )
    (setq file (write_dcl keys))
    
    (setq dcl_id (load_dialog file))
    (if (not (new_dialog "createNewSheet" dcl_id)) (exit))
    
    (foreach pair keys
      (action_tile (car pair) (strcat "(if (wcmatch (setq path (get_tile \"template\")) \"*.dwt\")
                                        (progn (setq ans (list path \"" (cadr pair) "\")) (done_dialog))
                                        (mode_tile \"template\" 2)
                                      )"
                              );; change *dwt for other extensions
      )
    )

    (action_tile "browse" "(setpath (get_tile \"template\"))")
    (start_dialog)
    (unload_dialog dcl_id)
    (vl-file-delete file)
    ans
  );defun
  
  ; Example list of layout tabs, replace with actual list
  (setq layoutTabsList (list "SAP_A1_H" "SAP_A1_VER")
        layoutinfo (chooseFileAndItem layoutTabsList)
  )
  ;;(create_drawing (car layoutinfo) (cadr layoutinfo))
)

 

but i dont know if your create_drawing function is working correctly

Edited by EnM4st3r
Link to comment
Share on other sites

Yes have what you want all done all debugged. 

 

image.png.82ed84f489ae2516903398d8d37e2c95.png

 

Two versions Grid based but can move and rotate, 2nd version is walk along a Pline or line making layouts. Both make rectangs at required scale matching layout title, can be moved rotated or erased before making layouts. Extras include layout renaming and re sheet numbering. Requires titleblock. Plus a few more programs built around task.

 

They were done for commercial clients and happy to discuss modifying to suit your needs, yes a small cost. 

 

 

layout1.png.9d03ab6cd7fbd5b4a128c15fbe0c31bf.png

 

 

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