Jump to content

Logic Issue: CENTERLINE Command Redefined with Layer Change


Recommended Posts

Posted

Can someone review the code and pick out the error in logic?

 

The BLADE IDE in BricsCAD shows no syntax errors: The code runs well.

The defined layer for the centerline is not activating when the command is run (nor returning to the older/previous layer)

There is obviously something out of order in the code logic and order.

 

Leaning on AI to learning programming is not a valid shortcut for study and experience!

Back to the books for me!

 

Thanks!

;;; CENTERLINE.lsp - Custom CENTERLINE assigning it to the CENTERLINE layer
;;; Author: CEH Assisted by AI
;;; Created on: 2025-12-16
;;; Last edited: 2025-12-16
;;; Description:
;;; BricsCAD CENTERLINE command redefined:
;;; Creates centerlines on layer CENTERLINE with CENTER2 linetype, ByLayer color, and ~0.008 inch lineweight
;;; 
;;; -------------------------  Program Start  -------------------------  
;;;
(defun c:CENTERLINE (/ oldlayer oldltype oldlw laydata)

  ;; Save current settings
  (setq oldlayer (getvar 'clayer)
        oldltype (getvar 'celtype)
        oldlw    (getvar 'celweight))

  ;; Ensure CENTERLINE layer exists
  (if (not (tblsearch "LAYER" "CENTERLINE"))
    (progn
      ;; Load CENTER2 linetype if not already loaded
      ;; Tries acad.lin (imperial) first, then acadiso.lin (metric)
      (cond
        ((not (tblsearch "LTYPE" "CENTER2"))
         (command "._-linetype" "_load" "CENTER2" "acad.lin" "")
        )
        ((not (tblsearch "LTYPE" "CENTER2"))
         (command "._-linetype" "_load" "CENTER2" "acadiso.lin" "")
        )
      )
      ;; Create the layer: CENTER2 linetype, ByLayer color, ByLayer lineweight initially
      (command "._-layer" "_make" "CENTERLINE" "_ltype" "CENTER2" "" "_color" "bylayer" "" "_lweight" "bylayer" "")
    )
    ;; If layer exists but linetype might not be set
    (progn
      (command "._-layer" "_set" "CENTERLINE" "_ltype" "CENTER2" "CENTERLINE" "")
    )
  )

  ;; Set current layer to CENTERLINE
  (setvar 'clayer "CENTERLINE")

  ;; Set current linetype and lineweight to ByLayer
  (setvar 'celtype "BYLAYER")
  (setvar 'celweight -1)  ; -1 = ByLayer

  ;; Set layer lineweight to exactly 0.008 inches (converted to internal 0.01 mm units)
  ;; 0.008 in = 0.2032 mm → 20.32 → integer 2032 (in hundredths of mm? Wait, no:
  ;; DXF code 370 stores lineweight in hundredths of mm (0.01 mm units), always mm.
  (setq laydata (entget (tblobjname "LAYER" "CENTERLINE")))
  (setq laydata (subst (cons 370 20) (assoc 370 laydata) laydata))  ; 20 = 0.20 mm ≈ 0.00787 in (very close to 0.008 in)
  (entmod laydata)

  ;; Run the original CENTERLINE command
  (command "._centerline")

  ;; Restore previous settings (optional)
  (setvar 'clayer oldlayer)
  (setvar 'celtype oldltype)
  (setvar 'celweight oldlw)

  (princ)
)

(princ "\nCENTERLINE command redefined: creates centerlines on layer CENTERLINE with CENTER2 linetype, ByLayer color, and ~0.008 inch lineweight.")
((princ "\nType CENTERLINE to run."))
(princ)

 

Posted

Don't write lisp command that use the same names or variables as default commands. That said looks like the default centerline command only outputs to layer 0. so you would need to change it to your layer. and pause pause to let you pick the lines.

 

  (command "_.centerline" pause pause)
  (if (setq e (entlast))
    (entmod
      (subst
        (cons 8 "CENTERLINES")        ; new layer name
        (assoc 8 (entget e))          ; old layer
        (entget e)
      )
    )
  )

 

Posted

mhupp,

 

Your suggestion and generous code submission are both very well received: Thank you!

I will rework the "robot" program code to include your superior code lines.

 

Questions

Outside of the important warning applied to LISP programming and variables,

  1. Can you or others give an example(s) of instances of how a programmer/customizer can modify default commands?
  2. Would this action be limited to creating macro code lines to assign to custom buttons?

Future  

 

After stalling out, I am now at a DWG-based design environment and will resume my LISP programming studies. I was recently encouraged by understanding how to use the built-in IDE.

 

P.S.: My thanks go out to the excellent developer and programmed systems just developed from a longtime fellow member of other forums and entrepreneur running https://cadprograms.ca/

 

Thanks,

Clint

  • Like 1
Posted

The " . "  is the only thing stopping your lisp from running the first 25 lines of your code in an infinite loop. 

 

Quote

Placed before a command name (e.g., ._CENTERLINE), it forces AutoCAD to use the built-in (native) command, even if that command has been redefined or overridden by a custom LISP or script.

 

If you gave that lisp to someone them typing centerline in the command prompt would run your code not the default centerline command. as it is "overwritten"

 

I'll post some examples tonight. but off the top of my head I created shortcuts to commands like

;;----------------------------------------------------------------------------;;
;; Create Cricle from picking 3 Points
(defun C:C3P () (command "._CIRCLE" "_3P"))

 

  • Like 1
Posted

mhupp - I look forward to learning from your examples (as avoiding catastrophes like infinite loops are really good things and things within our control in life.)

 

Thanks,

Clint

Posted
2 minutes ago, Lee Mac said:

Note that you can use my Layer Director to preset & reset a layer (including layer creation) for both standard and custom commands:

 

https://lee-mac.com/layerdirector.html

 

Ah, I should have known...

Yes sir, Lee.

 

I have downloaded the Layer Director and will set it up to skip the my beginner trials and tribulations in this case as I am in a time crunch.

I am responsible for pushing out a departmental CAD customization package where the custom file updating program was developed by another.

 

I understand that you are not taking on new projects.

 

Thanks for chiming in!

 

Clint

    

Posted

You can just use the following entry in the layer list to achieve the result you require:

("CENTERLINE"          "CENTERLINE"     ""                             7        "Continuous"           -3                 1                 nil         )

 

  • Like 1
Posted

Yes indeed, Lee. Thanks! I will use a "Center2" linetype instead of "Continuous" according to our standard practices.

 

Clint

Posted

Ok, just be aware that, by default, the CENTERLINE command already creates a line with linetype CENTER2 rather than ByLayer.

Posted
2 minutes ago, Lee Mac said:

Ok, just be aware that, by default, the CENTERLINE command already creates a line with linetype CENTER2 rather than ByLayer.

Oh yes, I was not aware of a BricsCAD-flavored Forum here and must use it (along with a copy of AutoCAD LT 2026 granted exclusively for development purposes to redefine inherited dynamic blocks) I understand that the patented dynamic block functionality is set to expire in 2027 here in the USA. The rest of the world now enjoys it in BricsCAD as I understand it.

 

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