1

I am looking for some function that will allow me to automatically "rebuild" 1 or more splines in AutoCAD. I have drawings that have hundreds of splines with 30-50 control vertices each. This makes drawings very slow to work with, especially when interacting directly with a group of these splines.

I have the basic code for what I want to do, but am not sure at this point how to use the cvrebuild command in AutoLISP. Using that command in the command line simply brings up a GUI. See the code below for what I have so far.

I simply want to invoke the cvrebuild command using the variables n_controlvertices and degree as arguments. The AutoLISP routine would go through one object at a time and rebuild them with the same parameters.

I apologize for the appearance of the code. Apparently AutoLISP does not play well with StackOverflow

;; Batch rebuild splines
;;defines command name and variables

(defun c:batchrebuild (/ ss n obj n_controlvertices degree)

;; asks for selection

(prompt "\nSelect splines to be rebuilt ." )

;;decides if any splines are selected, and if not selects all

(if (not (setq ss (ssget '((0 . "SPLINE"))))) (setq ss (ssget "_X" '((0 . "SPLINE")))) )

;;sets allowable entry to [2 (only nonzero) + 4 (only positive)]

(initget 6)

;;asks for number of fit points. if nothing is entered, it gives it the default value of 20

(setq n_controlvertices (getint "\nNumber of control vertices<20>: "))

(if (= n_controlvertices nil) (setq n_controlvertices 20) (setq n_controlvertices (fix n_controlvertices))
)

;;asks for degree of fit points. if nothing is entered, it gives it the default value of 3

(setq degree (getint "\nDegree of fit points<3>: "))

(if (= degree nil) (setq degree 3) (setq degree (fix degree))
)

(repeat (setq n (sslength ss))

(setq obj     (vlax-ename->vla-object (ssname ss (setq n (1- n))))


    ;;(command cvrebuild)
    ;;This is the part that I am not sure about


 )

(princ) )

1 Answers1

1

Here's a way. It calls the command line version of CVREBUILD (-CVREBUILD). It deals with system variables for the user inputs settings.

;; Batch rebuild splines
;;defines command name and variables

(defun c:batchrebuild (/ ss n obj n_controlvertices degree rebuild2doption rebuild2ddegree rebuild2dcv cmdecho)

  ;; asks for selection
  (prompt "\nSelect splines to be rebuilt.")

  ;;decides if any splines are selected, and if not selects all
  (or (setq ss (ssget '((0 . "SPLINE"))))
      (setq ss (ssget "_X" '((0 . "SPLINE"))))
  )
  ;; checks if the selection is not empty
  (if ss
    (progn
      ;;sets allowable entry to [2 (only nonzero) + 4 (only positive)
      (initget 6)

      ;;asks for number of fit points. if nothing is entered, it gives it the default value of 20
      (setq n_controlvertices
             (cond
               ((getint "\nNumber of control vertices<20>: "))
               (T 20)
             )
      )

      ;;asks for degree of fit points. if nothing is entered, it gives it the default value of 3
      (setq degree (cond
                     ((getint "\nDegree of fit points<3>: "))
                     (T 3)
                   )
      )

      ;; saves the sysvars current values
      (setq rebuild2doption (getvar "REBUILD2DOPTION")
            rebuild2ddegree (getvar "REBUILD2DDEGREE")
            rebuild2dcv     (getvar "REBUILD2DCV")
            cmdecho         (getvar "CMDECHO")
      )

      ;; sets the sysvars values according to user inputs
      (setvar "REBUILD2DOPTION" 1)
      (setvar "REBUILD2DDEGREE" degree)
      (setvar "REBUILD2DCV" n_controlvertices)
      (setvar "CMDECHO" 0)

      ;; rebuilds the selected splines
      (repeat (setq n (sslength ss))
        (command "_-cvrebuild" (ssname ss (setq n (1- n))))
      )

      ;; restores sysvars initial values
      (setvar "REBUILD2DOPTION" rebuild2doption)
      (setvar "REBUILD2DDEGREE" rebuild2ddegree)
      (setvar "REBUILD2DCV" rebuild2dcv)
      (setvar "CMDECHO" cmdecho)
    )
  )
  (princ)
)
gileCAD
  • 2,295
  • 1
  • 10
  • 10
  • Thanks so much for the help! I am new to AutoLISP, and some of this stuff is difficult to find answers for. Is there any documentation available for how to implement AutoCAD commands and their associated parameters? I am used to being able to search for documentation for each command in Java (parameters, options, instance variables, etc.). – Jeff Miller Oct 04 '17 at 15:17