au-s 发表于 2022-7-6 10:18:44

创建目录Visual Lisp

大家好。
我有点困在这里了。
 
我开始打字:

(if (not (vl-file-directory-p "U:\\Project1\\CAD_Xref_Files\\"))
(vl-mkdir "U:\\Project1\\CAD_Xref_Files\\"))

 
这适用于一个名为project1的项目。
 
如果我想在Project2中使用相同的lisp呢。我可以让它选择“主路径”吗?它可以是U:\\ProjectX\\n然后继续使用CAD\U Xref\U文件或我放在那里的任何文件。

Lee Mac 发表于 2022-7-6 10:27:49

这是我用来创建目录结构的内容:
 

;;-------------------=={ Make Directory }==-------------------;;
;;                                                            ;;
;;Creates a directory structure                           ;;
;;------------------------------------------------------------;;
;;Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;Arguments:                                                ;;
;;dir - the directory to create                           ;;
;;------------------------------------------------------------;;
;;Returns:T if directory creation is successful, else nil ;;
;;------------------------------------------------------------;;

(defun LM:MakeDirectory ( dir / MakeDirectory folders )
;; © Lee Mac 2010
;; (LM:MakeDirectory "C:\\Folder\\Subfolder")
(vl-load-com)

(defun MakeDirectory ( root folders )
   (if folders
   (
       (lambda ( dir ) (vl-mkdir dir)
         (MakeDirectory dir (cdr folders))
       )
       (strcat root "\\" (car folders))
   )
   )
)

(if (setq folders (LM:str->lst (vl-string-translate "/" "\\" dir) "\\"))
   (MakeDirectory (car folders) (cdr folders))
)
(vl-file-directory-p dir)
)

;;-------------------=={ String -> List }==-------------------;;
;;                                                            ;;
;;Separates a string into a list of strings using a         ;;
;;specified delimiter string                              ;;
;;------------------------------------------------------------;;
;;Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;Arguments:                                                ;;
;;str - string to process                                 ;;
;;del - delimiter by which to separate the string         ;;
;;------------------------------------------------------------;;
;;Returns:A list of strings                               ;;
;;------------------------------------------------------------;;

(defun LM:str->lst ( str del / pos )
;; © Lee Mac 2010
(if (setq pos (vl-string-search del str))
   (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
   (list str)
)
)

MSasu 发表于 2022-7-6 10:38:17

您必须一次验证/创建一级路径:
 

;;; STR2LST
;;; Transforms a string with separator into a list of strings
;;; Author: Gile
;;; Arguments
;;; str = the string
;;; sep = the separator pattern
(defunstr2lst(str sep / pos)
(if (setq pos (vl-string-search sep str))
   (cons (substr str 1 pos)
   (str2lst (substr str (+ (strlen sep) pos 1)) sep)
   )
   (list str)
)
)

(setq MyPath "U:\\Project1\\CAD_Xref_Files"
   MyPath (str2lst MyPath "\\")
   thePath (car MyPath))
(foreach PathItem (cdr MyPath)
(if (not (vl-file-directory-p (setq thePath (strcat thePath "\\" PathItem))))
(vl-mkdir thePath)
)
)

 
 
当做
米尔恰

au-s 发表于 2022-7-6 10:45:13

好吧,但我可能不太清楚。很抱歉
我想要的是让用户选择主路径。然后lisp继续生成CAD\U外部参照或路径\\to\\CAD\U外部参照\\

JohnM 发表于 2022-7-6 10:55:19

看看getfield函数。这将显示一个对话框,以便用户可以导航到路径或文件。

BlackBox 发表于 2022-7-6 11:00:29

 
使用什么标准来确定根路径(如果有)?
 
例如,如果所需的标准与激活图形相关,则可以使用(或提取部分)dwgprefix系统变量。

au-s 发表于 2022-7-6 11:03:44

是的,我尝试使用dwgprefix,然后我从dwg文件的位置创建了文本文件的相对路径。
我认为这是更好的解决方案。由于文件夹结构必须始终相同,因此它对用户是锁定的。在这种情况下,所有内容都存储在管理fodler中。
所以,这个问题至少解决了我的需要。
 
但问题是如何让用户选择一个项目路径,然后让lisp生成其他路径,例如:
用户选择其项目文件夹,lisp生成文件夹结构,即,
因此,如果用户选择文件夹,lisp会生成其他文件夹,因此完整路径将为P3/admin/cad,或者在第一种情况下为P1/admin/cad。
 
尽管我最终发现它在cad管理员问题上不是很好。因此,getvar dwgprefix对我帮助很大。
谢谢

BlackBox 发表于 2022-7-6 11:16:15

很乐意帮忙。
 
 
 
... 我是beleieve,JohnM已经回答了这个问题:
 

Lee Mac 发表于 2022-7-6 11:21:30

 
GetField(iLog)适合选择文件(顾名思义),我发现它更适合选择路径:
 

;;-------------------=={ Directory Dialog }==-----------------;;
;;                                                            ;;
;;Displays a dialog prompting the user to select a folder   ;;
;;------------------------------------------------------------;;
;;Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;Arguments:                                                ;;
;;msg- message to display at top of dialog                ;;
;;dir- root directory (or nil)                            ;;
;;flag - bit coded flag specifying dialog display settings;;
;;------------------------------------------------------------;;
;;Returns:Selected folder filepath, else nil            ;;
;;------------------------------------------------------------;;

(defun LM:DirectoryDialog ( msg dir flag / Shell HWND Fold Self Path ac )
;; © Lee Mac 2010

(setq Shell (vla-getInterfaceObject (setq ac (vlax-get-acad-object)) "Shell.Application")
       HWND(vl-catch-all-apply 'vla-get-HWND (list ac))
       Fold(vlax-invoke-method Shell 'BrowseForFolder (if (vl-catch-all-error-p HWND) 0 HWND)msg flag dir))
(vlax-release-object Shell)

(if Fold
   (progn
   (setq Self (vlax-get-property Fold 'Self) Path (vlax-get-property Self 'Path))
   (vlax-release-object Self)
   (vlax-release-object Fold)      
   
   (and (= "\\" (substr Path (strlen Path)))
          (setq Path (substr Path 1 (1- (strlen Path)))))
   )
)
Path
)

 
呼叫使用(或类似):
 
(LM:DirectoryDialog "Select Directory" nil 0)
 
页: [1]
查看完整版本: 创建目录Visual Lisp