anchor.diff:    );END foreach
   
     ; OPERATION - Return list of manholes in correct order
  -  (setq sorted_man_ch_list (acad_strlsort man_ch_list))
  +  (setq manholeChainageListSorted (acad_strlsort manholeChainageList))
   
     ; OPERATION - Rebuild data lists shorted by chainages
  -  (foreach ch sorted_man_ch_list
  +  (foreach ch manholeChainageListSorted
       (cond
         ((/= "" ch)
           (setq
  -          position (vl-position ch man_ch_list) 		                        ; get position of the chainage at man_ch_list (non-sorted list of chainages)
  -          sorted_man_db (append sorted_man_db (list (nth position man_db)))	; copy data associated to the current chainage to sorted_man_db
  +          ; get position of the chainage at manholeChainageList (non-sorted list of chainages)
  +          position (vl-position ch manholeChainageList)
  +          ; copy data associated to the current chainage to manholeDatabaseSorted
  +          manholeDatabaseSorted (append manholeDatabaseSorted (list (nth position manholeDatabase)))
           );END setq
         )
       );END cond
     );END foreach
  -  ;(princ "\nsorted_man_db = ")(princ sorted_man_db)
  -)
  -(defun DT:ExtarctManholeChainages ( / cntl ans )
  -
  +
  +  ; v0.2 - 2107.01.29 - Tidy up comments
  +  ;                   - Add layer to de manholeData
  +  ;                   - Split code into simpler functions
  +  ;                   - Wrap long section manhole entities in separate groups
  +  ; v0.1 - 2017.01.25 - Function fully rewritten
  +  ; v0.0 - 2016.08.08 - First issue
  +  ; Author: David Torralba
  +  ; Last revision: 2017.01.29
  +)
  +(defun DT:GetManholeDataForLongSection ( manholeEnt_name )
  +  ; Return manhole data list as so:
  +  ; (list
  +  ;   Manhole Block Chainage            = (nth 0 man_data)
  +  ;   Manhole Block "ID" attribute      = (nth 1 man_data)
  +  ;   Manhole Block "IL1" attribute     = (nth 2 man_data)
  +  ;   Manhole Block "IL2" attribute     = (nth 3 man_data)
  +  ;   Manhole Block "IL3" attribute     = (nth 4 man_data)
  +  ;   Manhole Block "IL4" attribute     = (nth 5 man_data)
  +  ;   Manhole Block "visibility state"  = (nth 6 man_data)   para tener el tamano de la arqueta (DN1200, etc.)
  +  ;   Manhole Block "manhole size"      = (nth 7 man_data)
  +  ;   Manhole Block layer               = (nth 8 man_data)  para luego poder dibujarlo en la misma capa en el long section
  +  ; );END list
  +
  +  (if manholeEnt_name
  +    (if (= "INSERT" (cdr (assoc 0 (entget manholeEnt_name))))
  +      ; Check if is a Manhole Block
  +      (if (= "W-Manhole" (substr (LM:effectivename (vlax-ename->vla-object manholeEnt_name)) 2 9))
  +        ; Return manhole data
  +        (list
  +          (DT:PK (vlax-ename->vla-object centrelineEnt_name) (cdr (assoc 10 (entget (cadr a)))))        ; Get manhole chainage
  +          (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "ID")                ; Extract Manhole Block "ID" attribute
  +          (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL1")               ; Extract Manhole Block "IL1" attribute
  +          (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL2")               ; Extract Manhole Block "IL2" attribute
  +          (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL3")               ; Extract Manhole Block "IL3" attribute
  +          (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL4")               ; Extract Manhole Block "IL4" attribute
  +          (LM:getvisibilitystate (vlax-ename->vla-object (cadr a)))                       ; Extract Manhole Block "visibility state"
  +          (cdr (assoc "Manhole size" (LM:getdynprops (vlax-ename->vla-object (cadr a))))) ; Extract Manhole Block "Manhole size"
  +          (cdr (assoc 8 (entget (cadr a))))                                               ; Extract Manhole Block layer
  +        );END list
  +        (progn (princ "\nERROR @ DT:GetManholeDataForLongSection > manholeEnt_name is not a manhole block\n")(princ) nil )
  +      );END if
  +      (progn (princ "\nERROR @ DT:GetManholeDataForLongSection > manholeEnt_name is not a block\n")(princ) nil )
  +    );END if
  +    (progn (princ "\nERROR @ DT:GetManholeDataForLongSection > manholeEnt_name = nil\n")(princ) nil )
  +  );END if
  +
  +  ; v0.0 - 2017.01.29 - First issue
  +  ; Author: David Torralba
  +  ; Last revision: 2017.01.29
  +)
  +(defun DT:ExtractManholeChainages ( / centrelineEnt_name ans )
     ; ALMOST FINISHED - Purpose: store the chainages of selected manholes, to later draw them in the long section with another function
     ; PRECAUTION - If any manhole is not within the line, the routine will assign them the chainage at the beginning or end of the centreline.
     ;
     ; INPUT - Ask user to select a centreline
  -  (while (not cntl)
  -    (if (not (setq cntl (car (entsel "\nSelect centreline: "))))
  +  (while (not centrelineEnt_name)
  +    (if (not (setq centrelineEnt_name (car (entsel "\nSelect centreline: "))))
         (princ "missed. Try again.")
         (progn
           (if
             (or
  -            (= "POLYLINE"   (cdr (assoc 0 (entget cntl))))
  -            (= "LWPOLYLINE" (cdr (assoc 0 (entget cntl))))
  +            (= "POLYLINE"   (cdr (assoc 0 (entget centrelineEnt_name))))
  +            (= "LWPOLYLINE" (cdr (assoc 0 (entget centrelineEnt_name))))
             );END or
             (princ "object selected.")
             (progn
               (princ "selected object is not a polyline. Try again.")
  -            (setq cntl nil)
  +            (setq centrelineEnt_name nil)
             );END progn
           );END if
         );END progn
  
positive.code: 
    (princ)
  
    ; v0.0 - 2017.01.25 - Function full restructuration
    ; v0.0 - 2016.08.08 - First issue
    ; Author: David Torralba
    ; Last revision: 2017.01.25
  )
  (defun DT:PK( VL_ent_name pt )
    (vlax-curve-getDistAtPoint VL_ent_name (vlax-curve-getClosestPointTo VL_ent_name pt))
  )
  (defun DT:SelectManholes( cntl
                          /
                          ss
                          man_db man_entry man_ch_list
                          sorted_man_db sorted_man_ch_list
                          pt ch id il1 il2 il3 il4 vis_sta
                          )
    ; Returns a sorted list (by chaiange) with the selected manhole data
    ;
    ; INPUT - Ask user to select manholes
    (setq
      ss (ssget '((0 . "INSERT")))
      man_db nil
    )
    (foreach a (ssnamex ss)
      (if (= 'ename (type (cadr a)))
        (progn
          ; Check if is a Manhole Block
          (if (= "W-Manhole" (substr (LM:effectivename (vlax-ename->vla-object (cadr a))) 2 9))
            (progn
              (setq
                man_entry (list
                            (DT:PK (vlax-ename->vla-object cntl) (cdr (assoc 10 (entget (cadr a)))))        ; Get manhole chainage
                            (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "ID")                ; Extract Manhole Block "ID" attribute
                            (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL1")               ; Extract Manhole Block "IL1" attribute
                            (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL2")               ; Extract Manhole Block "IL2" attribute
                            (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL3")               ; Extract Manhole Block "IL3" attribute
                            (LM:vl-getattributevalue (vlax-ename->vla-object (cadr a)) "IL4")               ; Extract Manhole Block "IL4" attribute
                            (LM:getvisibilitystate (vlax-ename->vla-object (cadr a)))                       ; Extract Manhole Block "visibility state"
                            (cdr (assoc "Manhole size" (LM:getdynprops (vlax-ename->vla-object (cadr a))))) ; Extract Manhole Block "Manhole size"
                          )
  
                man_db (append man_db (list man_entry))
                man_ch (LM:rtos (car man_entry) 2 5)
              );END setq
              ;(princ "\nman_db = ")(princ man_db)
              ; Manhole Block Chainage            = (nth 0 man_data)
              ; Manhole Block "ID" attribute      = (nth 1 man_data)
              ; Manhole Block "IL1" attribute     = (nth 2 man_data)
              ; Manhole Block "IL2" attribute     = (nth 3 man_data)
              ; Manhole Block "IL3" attribute     = (nth 4 man_data)
              ; Manhole Block "IL4" attribute     = (nth 5 man_data)
              ; Manhole Block "visibility state"  = (nth 6 man_data)   para tener el tamano de la arqueta (DN1200, etc.)
              ; Manhole Block "manhole size"      = (nth 7 man_data)
  
              ; OPERATION - Add zeros till have minimum 5 digits before dot "." to be able to sort them as strings later
              (cond
                ((= 1 (vl-string-search "." man_ch)) (setq man_ch (strcat "0000" man_ch)) )
                ((= 2 (vl-string-search "." man_ch)) (setq man_ch (strcat "000"  man_ch)) )
                ((= 3 (vl-string-search "." man_ch)) (setq man_ch (strcat "00"   man_ch)) )
                ((= 4 (vl-string-search "." man_ch)) (setq man_ch (strcat "0"    man_ch)) )
              );END cond
  
              ; OPERATION - Create list of chainages as strings
              (setq man_ch_list (append man_ch_list (list man_ch )) )
            );END progn
          );END if
        );END progn
  
negative.code: (defun c:1() (DT:ExtractManholeChainages))
  (defun c:2 ( / old_error old_sysvars ent_name startPoint datum verticalExageration)
    ; SAVE SETTINGS
    (save_environment (list "osmode" "attdia" "attreq"))
  