1

我在 C# 中设置了一个非常简单的 clojure 解释器,它加载一个 .clj 文件并使函数可在 AutoCAD 中使用。这很好用,但我想用更多的结构来设置它,这样我就可以“模块化”源文件,而不是拥有一个很棒的主文件(这是我目前可以让它工作的唯一方法) .

我已经尝试了各种方法,例如脚本中的导入、使用、需要加载和加载文件,并且还在 C# 脚本代码中加载多个文件,但我宁愿有一个主脚本,它在加载到所需的其他文件中时引用口译员。

这是我目前用来加载主文件的 C# 片段,

    Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
    clojure.lang.Compiler.loadFile(AppEntry.AppPath + "..\\Scripts\\main.clj");
    PromptResult res = ed.GetString("Enter a clojure command: ");
    // res should have the user entered command to invoke:
    var foo = clojure.lang.RT.var("main", res.StringResult);
    object o = foo.invoke();

这是我想在运行时加载的 2 个文件的示例,主文件将引用所有其他文件,

(ns main) ;; the main file that gets loaded into interpreter

(import 
    '(Teigha.DatabaseServices Line)
    '(Teigha.Geometry Point3d)
    '(dbtools add-to-db)) ;; my other 'script' file I would like imported for use

(defn add-line 
    []
    (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
         ;; call an external script file method
         (add-to-db line))) 

以及我想引用的那个,目前与主文件位于同一文件夹中,但希望在某个阶段将它们组织到子文件夹中。

(ns dbtools) ;; helper file/module

(import 
    '(Teigha.DatabaseServices Database SymbolUtilityServices 
                Transaction BlockTable BlockTableRecord OpenMode)
    '(Bricscad.ApplicationServices Application))

(defn add-to-db
    "Adds an AcDbEntity to ModelSpace of the current database
    Returns the ObjectId of the Entity added to the db."
    [entity]
    (let [ db (.. Application DocumentManager MdiActiveDocument Database)]
        (with-open [tr (.. db TransactionManager StartTransaction)]
            (let [  bt (.GetObject tr (.BlockTableId db) OpenMode/ForWrite)
                    btr(.GetObject tr (. SymbolUtilityServices GetBlockModelSpaceId db) OpenMode/ForWrite)]
                (let [id (.AppendEntity btr entity)]
                    (doto tr
                        (.AddNewlyCreatedDBObject entity true)
                        (.Commit))
                        id)))))

关于解决此问题的最佳方法的任何指导?谢谢。

编辑:我已经对主文件进行了以下更改,但我仍然愿意以更好的方式来执行此操作,例如 - 如何设置加载路径以匹配 main.clj 文件夹。这是更改后的文件以供参考:

(ns main) ;; the main file that gets loaded into interpreter

(load-file "C:\\path\\to\\dbtools.clj")
(require '[dbtools :as tools])

(import 
    '(Teigha.DatabaseServices Line)
    '(Teigha.Geometry Point3d))

(defn add-line []
    (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
         ;; call an external script file method
         (tools/add-to-db line)))
4

1 回答 1

0

对于Excel-REPL,我定义了以下帮助函数。

(import System.Environment)
(require '[clojure.string :as string])

(defn get-load-path []
  (set (string/split (Environment/GetEnvironmentVariable "CLOJURE_LOAD_PATH") #";")))

(defn set-load-path! [s]
  (let [
        new-path (apply str (interpose ";" s))
        ]
    (Environment/SetEnvironmentVariable "CLOJURE_LOAD_PATH" new-path)
    new-path))

然后我可以简单地更新加载路径

(set-load-path! (conj (get-load-path) "path/to/clojure/source/files"))

之后,我只需在 clj 文件中进行 require,ClojureCLR 就知道在哪里可以找到它们。

于 2015-03-19T07:46:08.280 回答