when trying to load this code as a .lsp in autocad i get an error saying malformed list on input. this is the code (defun LM:list-visibility-states (blk / blk-name blk-item is-dynamic has-ext-dict vis param-name param-value states)
(if (vlax-property-available-p blk 'Effectivename)
(progn
;; Get the effective name and retrieve the block object
(setq blk-name (vla-get-effectivename blk))
(setq blk-item (vla-item (vla-get-blocks (vla-get-document blk)) blk-name))
code
;; Check if the block is dynamic and has an extension dictionary
(setq is-dynamic (vla-get-isdynamicblock blk-item))
(setq has-ext-dict (vla-get-hasextensiondictionary blk-item))
(if (and (= :vlax-true is-dynamic) (= :vlax-true has-ext-dict))
(progn
;; Retrieve the visibility parameter from the extension dictionary
(setq vis (vl-some
(lambda (pair)
(if (and
(= 360 (car pair))
(= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair))))))
(cdr pair)))
(dictsearch
(vlax-vla-object->ename (vla-getextensiondictionary blk-item))
"ACAD_ENHANCEDBLOCK")))
(if vis
(progn
;; Get the parameter name and value
(setq param-name (cdr (assoc 301 (entget vis))))
(setq param-value (vla-getparameter blk-item param-name))
;; Get the visibility states
(setq states (vlax-get-property param-value 'VisibilityStates))
;; Output the visibility states
(princ "\nAvailable visibility states: ")
(foreach state states
(princ (strcat "\n" state)))
)
(princ "\nNo visibility parameter found."))
(princ "\nBlock is not dynamic or does not have an extension dictionary."))
(princ)
)
(princ "\nBlock does not have an effective name property.")
Assistant
User
the problem isnt with running the code but with loading the code
Assistant
User
i copied yr code straight from here and still get same error
Assistant
User
tried all that still same errors
Assistant
)
)
The error message "malformed list on input" suggests that there is a syntax issue in your code. In AutoLISP and Visual LISP, the parentheses are crucial for proper list structuring, and any mismatch or improper nesting can lead to various errors, including the one you're encountering.
Here are a few suggestions to help troubleshoot and fix the code:
Code Highlights and Corrections
Defining the Function: Ensure that the function is defined correctly without any mismatched parentheses.
Use of vlax-* Functions: Ensure that you are using the correct vlax-* functions to access properties and methods, as misuse can lead to runtime errors.
Properly Handling Lists: When working with lists, ensure that they are properly constructed.
Return Values: Make sure every conditional branch helps return a value or exception, helping to understand where the code fails.
Here’s the modified version of your code to fix the parentheses:
lisp
(defun LM:list-visibility-states (blk / blk-name blk-item is-dynamic has-ext-dict vis param-name param-value states)
(if (vlax-property-available-p blk 'Effectivename)
(progn
;; Get the effective name and retrieve the block object
(setq blk-name (vla-get-effectivename blk))
(setq blk-item (vla-item (vla-get-blocks (vla-get-document blk)) blk-name))
;; Check if the block is dynamic and has an extension dictionary
(setq is-dynamic (vla-get-isdynamicblock blk-item))
(setq has-ext-dict (vla-get-hasextensiondictionary blk-item))
(if (and (= :vlax-true is-dynamic) (= :vlax-true has-ext-dict))
(progn
;; Retrieve the visibility parameter from the extension dictionary
(setq vis (vl-some
(lambda (pair)
(if (and
(= 360 (car pair))
(= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair))))))
(cdr pair)))
(dictsearch
(vlax-vla-object->ename (vla-getextensiondictionary blk-item))
"ACAD_ENHANCEDBLOCK")))
(if vis
(progn
;; Get the parameter name and value
(setq param-name (cdr (assoc 301 (entget vis))))
(setq param-value (vla-getparameter blk-item param-name))
;; Get the visibility states
(setq states (vlax-get-property param-value 'VisibilityStates))
;; Output the visibility states
(princ "\nAvailable visibility states: ")
(foreach state states
(princ (strcat "\n" state)))
)
(princ "\nNo visibility parameter found."))
(princ "\nBlock is not dynamic or does not have an extension dictionary."))
(princ "\nBlock does not have an effective name property."))
)
Notes on Changes:
Ensure that every conditional is properly enclosed within its own parentheses.
Carefully verify that there are no missing or extra parentheses, especially at the beginning or end of each form.
Each if and progn has been given its own closing parentheses at appropriate locations.
Running the Code
Make sure you reload your AutoLISP file after making these changes:
In AutoCAD, use the APPLOAD command to load your modified .lsp file.
Call your function using (LM:list-visibility-states <your-block>), replacing <your-block> with the actual block reference you are testing.
If you continue to encounter issues, narrow down the problem by checking each step in the function to ensure that data is being processed correctly. Consider adding more debug print statements (using princ) to track variable values at different stages.
When you're encountering a "malformed list on input" error when trying to load the code in AutoCAD, this typically indicates that there is a problem with the syntax of the code itself. Since you mentioned that the issue arises when loading, here are some key areas to double-check:
1. Correct Parentheses Matching
Ensure that all parentheses are balanced. In the listed code, I see the parenthesis appear to be properly matched, but re-checking is essential.
2. Function Definition Syntax
The way the function is defined and structured can cause issues if there are mistakes. Here's how the function definition should look:
Ensure that there are no hidden special characters or unnecessary whitespace in the file, especially around the parentheses. Sometimes copying and pasting can introduce non-printing characters.
4. Using a Simple Test Case
Before testing the complex code, create a minimal test case to see if you can successfully load a simpler function. For example:
If this loads without issues, then the problem lies within the more complex function.
5. Character Encoding
Make sure the file is saved with the correct encoding (e.g., ANSI or UTF-8 without BOM). Sometimes, unconventional encodings can lead AutoCAD to misinterpret the file.
6. Check for Unmatched Quotes
Ensure that you haven’t mistakenly used unmatching quotes ('). They should be used properly to designate symbols in AutoLISP.
7. Line Endings
If you're editing the file on a different OS (e.g., creating it on Windows but using it on macOS or vice versa), ensure that the line endings are compatible. AutoLISP generally expects Unix-style (LF) line endings.
8. Error Location
If you're able to pinpoint where the "malformed list" error is occurring, look closely at that part of the code. The 'input' usually relates to how functions and lists are being constructed at that specific point.
Here’s a Cleaned-Up Code for Your Function
Revisiting your full function, let’s ensure it looks correctly formatted:
lisp
(defun LM:list-visibility-states (blk / blk-name blk-item is-dynamic has-ext-dict vis param-name param-value states)
(if (vlax-property-available-p blk 'Effectivename)
(progn
;; Get the effective name and retrieve the block object
(setq blk-name (vla-get-effectivename blk))
(setq blk-item (vla-item (vla-get-blocks (vla-get-document blk)) blk-name))
;; Check if the block is dynamic and has an extension dictionary
(setq is-dynamic (vla-get-isdynamicblock blk-item))
(setq has-ext-dict (vla-get-hasextensiondictionary blk-item))
(if (and (= :vlax-true is-dynamic) (= :vlax-true has-ext-dict))
(progn
;; Retrieve the visibility parameter from the extension dictionary
(setq vis (vl-some
(lambda (pair)
(if (and
(= 360 (car pair))
(= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair))))))
(cdr pair)))
(dictsearch
(vlax-vla-object->ename (vla-getextensiondictionary blk-item))
"ACAD_ENHANCEDBLOCK")))
(if vis
(progn
;; Get the parameter name and value
(setq param-name (cdr (assoc 301 (entget vis))))
(setq param-value (vla-getparameter blk-item param-name))
;; Get the visibility states
(setq states (vlax-get-property param-value 'VisibilityStates))
;; Output the visibility states
(princ "\nAvailable visibility states: ")
(foreach state states
(princ (strcat "\n" state)))
)
(princ "\nNo visibility parameter found."))
(princ "\nBlock is not dynamic or does not have an extension dictionary."))
(princ "\nBlock does not have an effective name property."))
)
Final Step
After ensuring all the above checks, try again to load the .lsp file through the AutoCAD command line:
Type APPLOAD in the command prompt.
Browse and select your .lsp file.
Click Load.
If the function loads successfully, you should not see the "malformed list" error anymore.
If you still encounter issues, consider breaking the code down into smaller chunks and loading them individually to identify which specific area is causing the error.
If you're still encountering a "malformed list on input" error when trying to load the function, despite copying the code directly, let's take a systematic approach to troubleshoot the issue.
Steps to Troubleshoot
Use a Text Editor:
Ensure you're using a plain text editor (like Notepad or Notepad++) to save your code. Avoid using word processors like Microsoft Word, as they introduce formatting that can cause issues.
Check for Extra Characters:
Make sure there are no extra spaces or hidden characters (especially after the parentheses or at the beginning of the line). This can happen when copying code from websites.
Test with Minimal Code:
Try loading an even more simplified version of the code to see if the problem lies within the complexity of the function.
lisp
(defun LM:test-function ()
(princ "\nHello, world!")
)
;; Call the test function at the end
(LM:test-function)
If this simpler function loads successfully, it indicates the core issue is later in the original function.
Manual Typing:
If the copy-pasting method continues to produce errors, try manually typing a simple version of the function. Sometimes, hidden characters get carried over in copy-pasting.
Validate Parentheses:
Confirm that each opening parenthesis has a closing counterpart, and check that everything is properly nested. In some editors, you can place your cursor next to a parenthesis to highlight its matching counterpart.
Use a New File:
Create a new .lsp file and then re-insert the function into that new file. Sometimes the existing file may have hidden formatting or encoding issues.
Check for Encoding:
Save your .lsp file as Plain Text with encoding set to ANSI or UTF-8 without BOM.
Loading Errors:
Make sure you don't have leftover definitions from previous loads that might still reside in memory. Reload AutoCAD or use the CLEAR command to clear any existing function definitions.
Testing with a Block:
The function relies on a block to operate. If you're trying to load the function without context, it could fail. You should ensure you have a valid block in your drawing if you intend to call the function afterward.
Simplified Code for Testing
Here's a minimal version of the original function with further simplifications. This function checks a single property on a block and returns a message.
lisp
(defun LM:simple-test (blk / is-dynamic)
(if (vlax-property-available-p blk 'Effectivename)
(progn
(setq is-dynamic (vla-get-isdynamicblock blk))
(princ (strcat "\nThe block is dynamic: " (if (= :vlax-true is-dynamic) "Yes" "No")))
)
(princ "\nBlock does not have an effective name property.")
)
)
;; Call the test function at the end
( LM:simple-test (car (entsel "\nSelect a block: ")) )
Final Steps
After ensuring that everything is clear and simple:
Load your new .lsp file with the simplified code.
If you see an error again, it’s likely due to how the environment is set up or possibly other LISP definitions in memory.
If this simplified version works, incrementally add back parts of your original function to identify where the exact issue lies. We can keep enhancing it based on what works. If any of these suggestions give you positive results, let me know, and we can proceed from there!
If you're consistently facing a "malformed list on input" error, there are a few factors we haven't yet explored thoroughly. Let's go through a detailed set of troubleshooting steps:
Alternative Steps to Diagnose the Issue
Error Identification:
The "malformed list" error can often be very specific to a particular line or set of characters. You might want to look at the exact line where the error occurs, which can sometimes be indicated in the AutoCAD command line. Add debug statements or reduce your function's complexity to narrow it down.
Simple Function Test:
One more step to test a simpler function. Try the following code in a new .lsp file to see if it loads without issues:
Load this code. If this loads without errors, then the issue likely resides in the specific syntax or content of the previous functions.
Check AutoCAD Command Line for Errors:
After you attempt to load a LISP file, check the AutoCAD command prompt closely for any additional details about where the error is happening. It sometimes specifies line numbers.
File Encoding:
You mentioned issues with copy-pasting. Regardless, please check your text editor settings for saving files. Save the .lsp file with:
Encoding: ANSI or UTF-8 without BOM
Line Ending: LF (Unix) or CRLF (Windows).
Manual Creation:
Instead of copying any code, please create a fresh new file and manually type a simple function that does nothing but print a message. This eliminates any leftover formatting from copied content.
Using Different Editors:
If using Notepad, try an alternate text editor like Notepad++, or even a code editor like Visual Studio Code, which allows you to handle plain text without unwanted formatting.
Step by Step Loading
If a simple function loads successfully, please proceed in these steps:
Incremental Building:
After your simpler load is successful, start reintroducing components of your initial code piece by piece. Begin with essential components (like basic variable setups), and test loading each step.
Add Debugging Statements:
If you encounter issues, add princ statements at different points in your function to see how far it gets before failing.
Clean Start with Core Components
Consider starting with a very basic dynamic block check as your initial function. Here’s a foundational version to check for dynamic properties in blocks:
lisp
(defun LM:check-dynamicblock (blk / blk-name is-dynamic)
(if (vlax-property-available-p blk 'Effectivename)
(progn
(setq blk-name (vla-get-effectivename blk))
(setq is-dynamic (vla-get-isdynamicblock blk))
(princ (strcat "\nBlock Name: " blk-name))
(princ (strcat "\nIs Dynamic: " (if (= :vlax-true is-dynamic) "Yes" "No")))
)
(princ "\nBlock does not have an effective name property.")
)
)
;; Call this function after loading
( LM:check-dynamicblock (car (entsel "\nSelect a block: ")) )
Conclusion
If you follow these steps and check with the simpler functions, you can gradually identify what is causing the loading error. If you continue to experience issues after following all the steps, I recommend looking into the AutoCAD installation or potentially file or application-level corruption that might affect how LISP files are loaded.
If this simplified code allows for a successful load, we can work together to gradually build back up to your original functionality while ensuring it remains valid.