【问题标题】:AutoLISP program giving inconsistent resultsAutoLISP 程序给出不一致的结果
【发布时间】:2014-08-19 21:40:51
【问题描述】:

软件:AutoCAD 2012(日语)

系统:MS Windows 7(日语)


情况

我制作了一个 .LSP 文件,它定义了一个新函数“C:MAKEATABLE”。

它要求用户选择一个 .CSV 文件,然后将其数据导入在 AutoCAD 中打开的图形文件。 .CSV 文件有两列:

  1. 序列号(整数)
  2. 数据(实数)

.CSV 文件的最后一行是“EOF”。

导入的数据应该是每个文本实体相互独立,除非它们从用户指定的插入点以表格方式排列。

现在,我的问题是,每当我加载该 .LSP 文件并调用我的函数时,结果并不总是相同的。 有时这些值会以所需的方式完美地出现;但很多时候,这些值看起来很混乱。值出现混乱的方式有以下三种。

  • 我尝试重新启动应用程序以及 PC,但没有 有用。
  • 我已将所有变量声明为本地变量,因此它们不是 受到干扰。

完整代码如下: Pastebin link also available

(defun c:makeatable ( / filename csvfile startpt data rdline digits lineno currentpt datapt datarelpt baserelpt indexbasept temp2 temp3 temp4 temp5 wide1 wide2 rowheight txtheight tempvar tmps)

    (setq filename (getfiled "Data File" "C:/Temp/drawings/" "csv" 128))    ;prompt user to open the file containing the data
    (setq csvfile (open filename "r"))                          ;open the file to read its contents
    (setq startpt (getpoint "\n Table insertion point: "))      ;prompt user to choose the insertion point

;------------;prompt user to input the parameters; if nil, default value is set;--------------------------;
;
;** This code is useful when default values are needed, so that the user doesn't have to enter them.    **;
;** If the values appear jumbled, kindly run the program again with appropriate values.         **;
;**                                                 **;


    (initget (+ 2 4))
    (if (not(setq txtheight (getreal "\n Enter Text height: ")))        
        (setq txtheight 4.0)
    )
    (princ)

    (initget (+ 2 4))
    (if (not(setq wide1 (getreal "\n Enter first column width: ")))     
        (setq wide1 15.0)
    )
    (princ)

    (initget (+ 2 4))
    (if (not(setq wide2 (getreal "\n Enter second column width: ")))        
        (setq wide2 30.0)
    )
    (princ)

    (initget (+ 2 4))
    (if (not(setq rowheight (getreal "\n Enter Row height: ")))     
        (setq rowheight 7.0)
    )
    (princ)
;----------------------------------------------------------------------------------------------------------;    

    (setq lineno 1) ;this var stores the line at which the program is currently at
    (setq digits 0) ;this var stores the (number of digits - 1) of the index

    (setq currentpt startpt)    ;initialize currentpt


;-------*------temporary variables for the arrangement of the data------*-------;

    (setq temp2  (/ (+ rowheight txtheight) 2)) 
    (setq temp3 (+ wide1 (* txtheight 2)))
    (setq temp4 (+ wide1 (/ wide2 5)))      
    (setq temp5 (- wide1 (/ wide1 20)))
    (setq tempvar (list 0 (* -1 rowheight) 0))

;-------------------------------------------------------------------------------;

    (setq datarelpt (list temp4 temp2 0))   ;these are relative points;
    (setq baserelpt (list temp5 temp2 0))

;------------------------------;while loop;-------------------------------------;

    (while (/= rdline "EOF")
        (cond   ((> lineno 9)       ;check the number of    ;
             (setq digits 1)        ;digits in the index    ;
            )
            ((> lineno 99)
              (setq digits 2)
            )
        );end cond



        (setq datapt (mapcar '+         ;these lines        ;
                currentpt       ;set the coordinates    ;
                datarelpt       ;for the data       ;
                )       ;           ;
        )

        (setq indexbasept (mapcar '+    ;these lines        ;
            currentpt       ;set the coordinates    ;
                baserelpt       ;for the index      ;
                )       ;           ;
        )

        (setq rdline (read-line csvfile))   ;read a line from the CSV file      
        (setq data (substr rdline (+ 3 digits)));extract the data from the read line    

        (setq tmp (command "STYLE" "MONO" "MONOTXT" "" "" "" "" "" ""))     ;makes the text monospace

    ;-----------------------------printing the values-----------------------;

        (command "text" datapt txtheight 0 data)    ;write the data

        (command "text" "_j" "_r" indexbasept txtheight 0 lineno)   ;write the index number
    ;-------------------------------------------------------------------;

        (setq lineno (1+ lineno))           ;increment line number

        (setq currentpt (mapcar '+      ;increment the      ;
                currentpt       ;current point      ;
                tempvar     ;coordinates        ;
                )       ;           ;
        )
    )  
;------------------------------;while loop ends;------------------------------------;

    (entdel (entlast))  ;to remove the extra index number printed at the end
    (close csvfile)     ;close the opened file
    (princ)         ;clean exit
)

我什至检查了插入文本的点 [使用 (princ datapt)(princ indexbasept)],发现它们正常。但是,当 AutoCAD 在屏幕上创建这些文本对象时,它们会占据相同的位置并变得混乱。

请告诉我我可能会出错的地方以及我现在应该做什么。

【问题讨论】:

    标签: csv autocad autolisp


    【解决方案1】:

    我的第一个想法是您需要在运行命令之前关闭 osnaps。

    (setq orig-osm (getvar "osmode"))
    (command "osmode" 0)
    ... the rest of your command ...
    (command "osmode" orig-osm)
    

    根据您的约束,我认为您还可以将表格列生成为多行文本(多行文本)并在创建后将它们分解以创建单独的文本对象。您可以使用 (ssget "L") 对添加到数据库的最后一个元素进行操作。

    【讨论】:

    • 感谢您的回复,一定会尝试的。另请注意,完全相同的程序在我运行英文版软件的其他系统上运行良好。
    • 非常感谢。我对这个问题完全感到困惑;你是救生员。这么简单的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多