Get from Tree: seleceted node and descendants

Started by Jim Short, March 15, 2009, 01:21:41 PM

Previous topic - Next topic

Jim Short

I am using a Tree control for an operational program. The key of each node is also used to create a dictionary xrecord. If one desires to delete a node from the Tree, the control manages removing the descendants automatically. But wait! There are xrecords that need to be deleted as well. Since the keys of the tree nodes were designed to be the same as the keys of the xrecords, all we need is a list of keys belonging to the selected node.

Here is the first working version I came up with:

Code (autolisp) Select

;;returns selected node and descendants
(defun tcam:getTreeBranch (cntrlName / rslt)
  (tcam:BranchBug (dcl_Tree_GetSelectedItem cntrlName))
  (reverse rslt)
)
(defun tcam:BranchBug (nodekey / childkey sibkey tmpkey)
  (setq tmpkey nodekey
rslt (cons nodekey rslt))
  (if (dcl_Tree_ItemHasChildren cntrlName nodekey)
    (cond
      ((setq childkey (dcl_tree_getfirstchilditem cntrlName nodekey))
       (tcam:BranchBug childkey)
       (setq tmpkey childkey)
       (while
(setq sibkey (dcl_tree_getnextsiblingitem cntrlName tmpkey))
  (tcam:BranchBug sibkey)
  (setq tmpkey sibkey))))))

;***proof***
;Select Setup node

(tcam:getTreeBranch Onset_Outline_Tree)

("Setup{" "Setup{Info"
  "Setup{Datum"
  "Setup{Material"
  "Setup{Machine"
  "Setup{Tools"
)

;Select Pgm node

(tcam:getTreeBranch Onset_Outline_Tree)

("Pgm [1]{"
  "Pgm [1]{BoreHole [1]{"
  "Pgm [1]{BoreHole [1]{SpotDrill [1]"
  "Pgm [1]{BoreHole [1]{Drill [1]"
  "Pgm [1]{BoreHole [1]{Bore [1]"
  "Pgm [1]{Pocket [1]{"
  "Pgm [1]{Pocket [1]{Rough [1]"
  "Pgm [1]{Pocket [1]{Finish [1]"
  "Pgm [1]{Pocket [1]{EdgeBreak [1]"
)


Jim Short

Fred Tomke

Quote from: Jim Short on March 20, 2009, 04:31:18 PM
Hey Fred , did you look at the get descendants routine?
Jim

Yes, Jim, I had a short view. It is useful if you don't keep the treeview data in a separate list. But that's the thing I do: when building up the tree view I need lists of parents and childs. Because I know that I will need the information during the runtime I keep and maintain the lists until the form will be closed and the function will return. In most cases I use a tree view to visualize hierarchical database structures (using ADO DB). Every tree item gets the key as string (unique id as long combined with a prefix of item type) for later identifying. In some other cases I do also visualize dictionary data in trees and I use the object handle as key (just imagine a book as top level item as AcDbDictionary, a chapter as its child item as AcDbDictionary and its grand childs as AcDbXrecord.

But as I said if anybody has no chance to keep the list it is very useful.

Have a good night!
Fred
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

Jim Short

Fred, Thanks for your explanation. It's nice to see how others solve problems.
Jim
Jim Short