You must clean drawing objects before building a topology with them.
Drawing cleanup extends undershoots, snaps clustered nodes, removes duplicates, simplifies linear objects, and corrects other errors.
Let's look at this in two phases, preparing the cleanup model, which ends with a call to tpm_cleaninit, and executing the cleanup, which begins with a call to tpm_cleanstart.
To prepare the cleanup model
- Allocate memory for the cleanup model. Use tpm_cleanalloc.
- Allocate memory for cleanup variables, which specify properties for the cleanup process. The variables are initialized to their default values. Use tpm_varalloc.
- Get a selection set of objects to be cleaned (the include set).
- Set cleanup variables using tpm_varset with the clean_var_id that you allocated in step 2. A few of these variables specify cleanup actions, but most of them specify how cleanup actions will be performed.
- Call tpm_cleaninit to add cleanup variables and the selection set of objects to clean to the cleanup model.
(setq clean_id (tpm_cleanalloc))
(setq clean_var_id (tpm_varalloc))
If you will be specifying an explicit list of cleanup actions (you create and manage this list with calls to tpm_cleanactionlistins and related functions), also allocate memory for cleanup action variables, which specify properties for individual actions. Again use tpm_varalloc.
(setq action_var_id (tpm_varalloc))
(prompt "\nSelect objects to clean.") (setq ss_clean (ssget))
You can also get a selection set of objects to be anchored (the anchor set). Anchored objects are not repositioned by the cleanup process, but remain fixed while others are repositioned around them.
(prompt "\nSelect objects to anchor.") (setq ss_anchor (ssget))
The ssget function prompts the user to select objects and returns a selection set.
; set some cleanup variables (tpm_varset clean_var_id "MAINTAIN_MARKERS" 1) (tpm_varset clean_var_id "CLEAN_TOL" 4.21) (tpm_varset clean_var_id "ANCHOROBJS_LAYERS" "Layer1")
Before setting cleanup variables, you can load a cleanup profile if you saved one previously, and in that way set many variables at once. Use tpm_cleanprofileload.
(setq result (tpm_cleanprofileload clean_var_id "C:\\profile.dpf"))
If you specify an explicit list of cleanup actions, note that those will be the only actions performed. Cleanup actions specified by the variables NODE_ERROR, LINK_ERROR, and GENERALIZE will be ignored, as well as any setting specific to them only, such as CORRIDOR's, which defines the tolerance for GENERALIZE.
Using an action list is the best way to specify cleanup actions, because you can specify the order in which they execute, and you can include the same action more than once. Using variables to specify cleanup actions is an older technique, which is still supported for the sake of older scripts, but it is deprecated from AutoCAD Map 6 onward.
Note When you insert the Simplify Objects action (clean group type 128), it is always listed first, and you cannot insert it more than once.
With an explicit list of cleanup actions, note that certain individual actions can have individual tolerance settings (and in some cases, other settings also). See Cleanup Action Variables. When you are about to insert an action into the action list, you can use tpm_varset with the action_var_id that you allocated in step 2 to set variables for this action before calling tpm_cleanactionlistins. You can continually reset and reuse the same set of cleanup action variables with each action that you insert.
; insert a cleanup action into the action list ; first set a tolerance for this action (tpm_varset action_var_id "CLEAN_TOL" 2.2) ; with the action list referenced by clean_var_id... ; * insert at the first position (position 0) ; * insert Erase Short Objects (action 1) ; * with the options referenced by action_var_id (tpm_cleanactionlistins clean_var_id 0 1 action_var_id)
At any point while you are setting cleanup variables, or after you have finished, you can save the current cleanup profile using tpm_cleanprofilesave.
(setq result (tpm_cleanprofilesave clean_var_id "C:\\profile.dpf"))
Note that saved profiles are XML files. You can view or edit them in a text editor as you can with saved queries (which are AutoLISP scripts). See Editing Query Files.
(setq result (tpm_cleaninit clean_id clean_var_id ss_clean))
If you have collected a selection set of objects to be anchored, first call tpm_cleaninitanchorset before calling tpm_cleaninit.
(setq result (tpm_cleaninitanchorset clean_id clean_var_id ss_anchor))
The cleanup model is now complete.
To execute the cleanup
- Begin the cleanup process with tpm_cleanstart.
- Execute cleanup actions (process cleanup groups) until cleanup is complete. With each cleanup group, with each error, mark and fix it.
- Update the drawing with tpm_cleanend.
(setq result (tpm_cleanstart clean_id))
(tpm_cleangroupnext clean_id) (while (not (tpm_cleancomplete clean_id)) ; count errors in this group (setq i (tpm_cleangroupqty clean_id)) ; process each error (while (>= (setq i (1- i)) 0) ; with the current error (tpm_cleanerrorcur clean_id i) ; mark it (tpm_cleanerrormark clean_id) ; clean it (tpm_cleanerrorfix clean_id) ) (tpm_cleangroupnext clean_id) )
(tpm_cleanend clean_id)
To clear the cleanup model without updating the drawing, use tpm_cleancancel.