I want to extract certain information from the comparison of the information stored in a drawing-table or table object, as you prefer to call it, if the comparison succeeds then store the related values into variables. I am new to Visual lisp or Auto Lisp. So please can you help me with that issue and explain me step by step please.
So for example, if my table have in the first column D1 I want to store the information in the next three columns next to it but in the same row.
So in this example it would be 132156, 432 y 11 the numbers to be stored in three different variables or an array. Please help me and explain me step by step the possible solutions, I am really new to Lisp
first You need to get table. You can ask user to select one, for example like this:
(setq table (vlax-ename->vla-object (car (entsel ))) )
You should remember to catch error if user wouldn't like to select .
Also You should check if user select table not some other enity. but now let's imagine that user select table
so now You may try this
(setq columns (vlax-get-property table 'Columns))
(setq rows (vlax-get-property table 'rows))
(setq row 1 ) ; 0 is header
(repeat rows
(setq vals nil)
(setq column 0)
(setq txtval (vlax-invoke-method table 'GetText row column ))
; now we have value from first cell in row.
; and now You can go at least two ways.
; 1 check value and make Your analyse, read values from other columns or anything You need
; 2 build array of all values from all cells and after that analyse only array of texts (remove dependency from object table)
; for this sample I choose 1 way.
(if (= txtval "D1") (progn
(repeat 3 ; because You "want to store the information in the next three columns"
(setq column (1+ column))
(setq vals ( append vals (list (vlax-invoke-method table 'GetText row column ))))
)
))
(if (not (null vals )) (progn
(setq arrayOfVals (append arrayOfVals (list vals)))
))
(setq row (1+ row ))
)
(print arrayOfVals)
Related
I have two texts associated to a line. Because the texts represent some data of the line they are always considered children of the line and visible next to it. Through some lisp routines, if the data of the line change, the text entities reflect the change by changing their text. For that I have stored the handle of the line to each text as xdata and vice versa, e.g. the handles of the texts into the line.
The problem arises when I copy the line with the texts where each one gets a new handle but the stored xdata are giving the old handles which leads to further problems. I thought the vlr-copied reactor could solve my problem but since I am not very proficient with reactors I cant seem to make it work.
Could someone point me to the right direction? I found this
http://www.theswamp.org/index.php?topic=42654.0
but I cannot understand when I make a selection set of lines but also including non relevant other entities, how to pass the correct selection set to the reactor and get the handles updated.
Any suggestion appreciated. Thank you.
Firstly, you need to decide on the behaviour that you want the objects to exhibit assuming that either object (text or line) is copied independently of the other. Since the two objects are linked, you may need to decide which object is the 'master' and which is the 'slave'.
For example, if the text object is copied into empty space, you might decide that the resulting copy should be deleted, since there is no line to which it could refer. Whereas, if the line is copied into empty space, you might decide to replicate the associated text object and position it relative to the new line.
This is the approach that I followed when developing my Associative Textbox application (which is essentially solving the same problem of associating two objects in a drawing - in my case, a text object and a bounding frame).
In my application, I use a separate Object Reactor to handle the modification events for the text object and textbox respectively:
(vlr-object-reactor txt "tbox-textreactor"
'(
(:vlr-modified . tbox:textcallback)
(:vlr-copied . tbox:textcopied)
)
)
(vlr-object-reactor box "tbox-tboxreactor"
'(
(:vlr-modified . tbox:tboxcallback)
(:vlr-copied . tbox:tboxcopied)
)
)
Similar to your setup, these are built & configured on program load using Extended Entity Data (xData) attached to both the text and textbox.
When the Copy event for the texbox is fired (evaluating the tbox:tboxcopied callback function), I decide that a textbox cannot live without the text it encloses, and so I delete the orphan textbox from the drawing.
However, the most important point that you have to remember when working with object reactors is that you cannot modify the owner of an object reactor within its own callback function.
As such, for all modification events in which I need to modify the owner of the event, I generate a temporary Command Reactor which will fire after the object has been modified, so as to ensure that the object is not locked for modification.
For example, for the textbox copy event, I use the following:
(defun tbox:tboxcopied ( owner reactor params )
(if (/= 0 (car params))
(progn
(setq tbox:owner (append tbox:owner (list (car params))))
(vlr-command-reactor "tbox-tboxcopiedcommreactor"
'(
(:vlr-commandended . tbox:tboxcopiedcommandended)
(:vlr-commandcancelled . tbox:tboxcopiedcommandcancelled)
(:vlr-commandfailed . tbox:tboxcopiedcommandcancelled)
)
)
)
)
(princ)
)
I then remove this temporary Command Reactor within any of its own callback functions so as to prevent the propagation of redundant reactors in the drawing:
(defun tbox:tboxcopiedcommandended ( reactor params / ent )
(vlr-remove reactor) ;; <----- Remove temporary Command Reactor
(if
(and
(setq ent (car tbox:owner))
(member (cdr (assoc 0 (entget ent))) '("CIRCLE" "LWPOLYLINE"))
)
(entdel ent) ;; <----- Delete orphan textbox
)
(setq tbox:owner (cdr tbox:owner))
(princ)
)
Whereas, when the text is copied, I recreate the surrounding textbox and build the new association (again, generating a temporary Command Reactor to facilitate modification of the text object itself):
(defun tbox:textcopied ( owner reactor params )
(if (/= 0 (car params))
(progn
(setq tbox:owner (append tbox:owner (list (car params))))
(vlr-command-reactor "tbox-textcopiedcommreactor"
'(
(:vlr-commandended . tbox:textcopiedcommandended)
(:vlr-commandcancelled . tbox:textcopiedcommandcancelled)
(:vlr-commandfailed . tbox:textcopiedcommandcancelled)
)
)
)
)
(princ)
)
...and recreate the appropriate textbox as part of the callback function for the temporary Command Reactor:
(defun tbox:textcopiedcommandended ( reactor params / box ent enx val )
(vlr-remove reactor) ;; <----- Remove temporary Command Reactor
(if
(and
(setq ent (car tbox:owner))
(setq enx (entget ent (list tbox:app)))
(member (cdr (assoc 0 enx)) '("TEXT" "MTEXT"))
(setq val (cdadr (assoc -3 enx)))
(setq box (tbox:createbox enx (cdr (assoc 1000 val)) (cdr (assoc 1040 val))))
)
(progn
(entmod
(append (vl-remove (assoc 40 enx) (entget ent))
(list
(list -3
(list tbox:app
'(1002 . "{")
(cons 1005 (cdr (assoc 5 (entget box))))
(assoc 1000 val)
(assoc 1040 val)
'(1002 . "}")
)
)
)
)
)
(if (= 'vlr-object-reactor (type tbox:textreactor))
(vlr-owner-add tbox:textreactor (vlax-ename->vla-object ent))
)
(if (= 'vlr-object-reactor (type tbox:tboxreactor))
(vlr-owner-add tbox:tboxreactor (vlax-ename->vla-object box))
)
)
)
(setq tbox:owner (cdr tbox:owner))
(princ)
)
And the methods I have described above is the approach that I would recommend for your scenario:
When the text is copied, delete the resulting orphaned text object; when the line is copied, create a corresponding text object and build the association between the copied line & new text object.
I am trying to parse a comma separated string with Common lisp with the function below. For (separate-on-comma "a,b,c") I would expect the output ("a" "b" "c") but instead I get ("c" "c" "c").
What do I miss here?
(defun separate-on-comma (line)
(loop
with fields = ()
with field = (make-array 0 :element-type 'character :adjustable t :fill-pointer 0)
for x across line
when (char= x #\,)
do (push field fields)
and do (setf (fill-pointer field) 0)
else
do (vector-push-extend x field)
finally
(push field fields)
(return fields)))
Thanks.
You are working only with one field array. That's okay. But you better copy it when you push it to the fields list variable. Use copy-seq.
Alternatively create a new field array once you pushed the old one onto the fields list.
I am Tasked (in exercise 8) with creating a function in Intermediate Student Language (Racket) that receives a list of numbers and a list of lists of numbers; each list is the same length. Name the first list breakpoints and the second LoR (list of rows). This function should be defined using map and should filter each row in LoR so that only numbers larger than that the n'th row in LoR only contains values larger than the n'th value in breakpoints-- here is an example for clarity:
(define breakpoints (list 7 2 15))
(define LoR (list (list 3 25 13)
(list 1 2 11)
(list 22 4 8)))
would output...
(list (list 25 13) (list 11) (list 22))
Doing this without using map would be fine and I understand the problem in that sense, but I am having trouble figuring out how to use map. I was thinking recursively in the sense that if the list of rows is not empty, I would cons the (filtered first row) to the (recursive call of the function using the rest of breakpoints and LoR) as so:
(define (parallel-filter breakpoints LoR)
(cond((empty? breakpoints) empty)
(else (cons ...
(parallel-filter (rest breakpoints) (rest LoR))))))
However, I'm not sure how to replace the ellipse with a map statement that would make the function work correctly-- as I understand, map's first parameter must be a one-parameter function and I'm not sure how to use map for this purpose. How do I proceed?
edited to correct output
We can solve this problem by combining map and filter, but first bear in mind this:
You can't use just map, because it will always return a list of the same length as the original. For excluding some elements we must use filter (or alternatively: use foldr to implement our own version of filter)
map can receive more than one list as parameter, as long as its function receives enough parameters - one from each list
Your sample output is wrong for the first sublist
With all of the above in mind, here's one possible solution:
(define (parallel-filter breakpoints LoR)
(map (lambda (brk lst) ; `brk` is from `breakpoints` and `lst` is from `LoR`
(filter (lambda (ele) (> ele brk)) ; filter each sublist from `LoR`
lst))
breakpoints
LoR))
It works as expected:
(parallel-filter breakpoints LoR)
=> (list (list 25 13) (list 11) (list 22))
I'm very new to elisp and I'm trying to adapt some existing code.
While looping over a table (generated by the orgmode function org-clock-get-table-data) I try the following:
((equal column "Project") (insert (cdr row)))
which yeilds the following in the Messages buffer:
cond: Wrong type argument: char-or-string-p, (#("Verify CalTime accruals for vacation/sick" 0 41
(fontified t org-category #("Admin" 0 5 (fontified t org-category "Admin" org-category-position 32
line-prefix nil wrap-prefix nil ...)) org-category-position 32 line-prefix #("*" 0 1 (face org-
hide)) wrap-prefix #(" " 0 4 (face org-indent)) ...)) nil 30 (("wps" . "Administration")))
The value that I want to insert is "Administration" so I try this
((equal column "Project") (insert (nth 4 row)))
which yeilds the following in the minibuffer
Wrong type argument, char-or-string-p, (("wps" . "Administration"))
Can someone tell me how I can insert the string "Administration"?
EDIT
Thanks Wes and Drew:
(cdar row)
Doing the above yields the first element of the complex list that I saw in the Messages buffer earlier:
Verify CalTime accruals for vaction/sick
row seems to be a complex list of lists:
((equal column "Project") (insert (car row))); yeilds ^B
((equal column "Project") (insert (cdr row)))
yields:
cond: Wrong type argument: char-or-string-p, (#("Verify CalTime accruals for vacation/sick" 0 41
(fontified t org-category #("Admin" 0 5 (fontified t org-category "Admin" org-category-position 32
line-prefix nil wrap-prefix nil ...)) org-category-position 32 line-prefix #("*" 0 1 (face org-
hide)) wrap-prefix #(" " 0 4 (face org-indent)) ...)) nil 30 (("wps" . "Administration")))
I think each "#" in the output above represents a list item. The fact that (nth 4 row) gives the error output shown above supports this and suggests that something like this might work:
((equal column "Project") (insert (nth 2 (nth 4 row))))
;yeilds wrong type argument, char-or-string-p, nil
So there is probably some function that I need to use to decode that 4th list item....
That 4th element is the orgmode property that has been assigned to the clocktable entry on the row we are parsing. This list of properties is defined in my .emacs:
(setq org-global-properties
;; WPS = Web Platform Services. Time tracking for Google Sheet begun with CalTime Migration
'(("wps_ALL".
"Administration
ASG-Consulting
Chanc-Office-Website
")))
Instead of the assoc + cdr combination, you can also use assoc-default:
ELISP> (assoc-default "one" '(("one" . "two")))
=> "two"
Note that lists of the form (("wps" . "Administration") ("foo" . "bar")) are typically "alists", and so you may need to handle the situation where there is more than one item in your list.
Read: C-hig (elisp) Association Lists RET
and also: (elisp) Dotted Pair Notation
You may obtain a keyed item from an alist with (assoc) or (assq) depending on the form of equality needed for the test. Equivalent strings are not equal objects in elisp, so in this case you want assoc rather than assq.
That gives you the entire (KEY . VALUE) form as a result, and you obtain the cdr of that as usual; hence:
(let ((my-alist '(("wps" . "Administration") ("foo" . "bar"))))
(cdr (assoc "wps" my-alist)))
yields: "Administration"
Lists are complex to think about in elisp. Until you've done enough of them that you stop thinking about them. Ha ha. But read up on the functions like car (picks the first element in a list) and cdr (picks the rest of the rest) to get started. And then you can chain things by combining letters in short sequences at least. So the answer you want is:
(cdar row)
I'm attempting to write an elisp function(s) to extract text from org files. Specifically, I want to be able to convert the values in an org-table into a list of lists, so that I can populate org-feeds-alist at startup time from a file outside of my .emacs.
My functions look like this:
(defun·org-config-parse-get-table-rows·(file)
"Return·table·rows·minus·header"
(with-temp-buffer
··(insert-file-contents·file)
··(cddr·(org-element-map·(org-element-parse-buffer)·'(table-row)·'identity))))
(defun·org-config-parse-get-table-cells·(file)
··(org-element-map·(org-config-parse-get-table-rows·file)·'(table-cell)·'identity))
I am testing it with the following table:
|·Name···········|·Fav·Color·|·Age·|·Sex····|
|----------------+-----------+-----+--------|
|·Jim············|·Blue······|··19·|·Male···|
|·Jane···········|·Green·····|··18·|·Female·|
|·Ort'hlrothl'gr·|·Unkown····|·-29·|·???····|
The closest I am able to get to retrieving the text of a single cell in the table is with the following:
(car·(last·(car·(org-config-parse-get-table-cells·"test.org"))))
which evaluates to:
#("Jim" 0 3
(:parent
...
Given a list of org elements returned by org-element-parse-buffer, what is the proper way of extracting the text of those elements as a string?
Thanks in advance.
As I understood, you want a list of lists from an org-mode table.
Here are the relevant bits of org-table-export:
(defun orgtbl->lists ()
(unless (org-at-table-p) (user-error "No table at point"))
(org-table-align)
(let* ((beg (org-table-begin))
(end (org-table-end))
(txt (buffer-substring-no-properties beg end))
(skip nil)
(lines (nthcdr 0 (org-split-string txt "[ \t]*\n[ \t]*")))
(lines (org-table-clean-before-export lines))
(i0 (if org-table-clean-did-remove-column 2 1))
(table (mapcar
(lambda (x)
(if (string-match org-table-hline-regexp x)
'hline
(org-remove-by-index
(org-split-string (org-trim x) "\\s-*|\\s-*")
nil i0)))
lines)))
table))
You can look at the original function if something is still missing.