- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I have been working on a function that can copy Object Data from one object to another object (without OD).
The first step was quite easy (and succesful) where I find a source and target object. I list all the different tables attached to the source object, then attach them the target object and copy the column data one by one into the corresponding table of the target object.
In this step I'm trying to expand the function to account for the fact that the source object can have multiple records of the same table attached, and I'm trying to 'loop' my way around to getting all the records copied from the source to the target object. (a future step would be to build in a 'security' : what if the target object already has OD records of the to be copied tables attached to it?)
I've hit a snag though: somehow I manage to get the tables and their definition, and I can also find how many records are attached for each table. But when trying to copy the data to the target object, the right amount of records are attached to my target object, but only the first one (recnum=0) gets filled in. Other records remain blank.
This is my code so far (I know there must be a way to make it more lean, or to bypass the loops within loops) at the moment my concern is WHY the recnum>0 are not being copied/filled.
Thank you for your time and effort.
;----------------------------------------------
; Error Handler
;----------------------------------------------
(defun mhv:Error (st)
; Note in addition to errors, also triggered any time user presses Esc
; only way to distinguish, is the passed error string st
; on English installs: (exit) = "Function cancelled", Esc = "quit / exit abort"
(if
(not
(member st (list "Function cancelled" "quit / exit abort ")
) ; so if it is not one of these error messages, then give me a code backtrace:
)
(vl-bt)
) ; if statement
(princ)
) ; Error Handler
;----------------------------------------------
;--------------------------------------------
; copy OD table + value from 1 object to another
;--------------------------------------------
(defun c:OD_COPY ( / *error* sourceobj tablelist tablelistlength tablelistsort tableoccurence tablecnt destobj x y tabledef columns
quantity cnt ycnt rvalue fieldname)
; get a source object (with OD) name
; get all the tables for the object
; for each table
; get all the OD records + values
; get a destination object
; attach all the table to the object
; for each table
; set all the OD record values
;
(setq *error* mhv:Error) ; load the Error Handler
(if
(setq sourceobj (car (entsel "\nChoose a source object: "))
tablelist (ade_odgettables sourceobj) ; get a list of all the attached table names
tablecnt 0
destobj (car (entsel "\nChoose a destination object: "))
)
(progn
(foreach table tablelist
(setq tableoccurence
(cons table (ade_odrecordqty sourceobj table))
)
(setq tablelistsort (cons tableoccurence tablelistsort)) ; set a list with (table . occurrence) (table . occurrence)
) ; end of foreach
(setq tablelistlength (length tablelistsort))
(while
(< tablecnt tablelistlength)
(setq x (car (nth tablecnt tablelistsort)) ; get a single tablename
y (cdr (nth tablecnt tablelistsort)) ; get the occurence of that tablename
tabledef (ade_odtabledefn x) ; get the table definition
columns (cdr (caddr tabledef)) ; get the records def (columns) from tabledef
quantity (length columns) ; how many records are there in the tabledef
cnt 0
ycnt 0
) ; end of setq
(if
(null (member x (ade_odgettables destobj)))
(repeat y (ade_odaddrecord destobj x))
) ; end of if (if the destination object does not have the tablename, attach it y times, otherwise do nil)
; if statement needs to be modified: what to do if destobj already has tables attached?
(while
(< ycnt y)
(while
(< cnt quantity)
(setq rvalue nil
fieldname (cdr (car (nth cnt columns))) ; get the name of the record in the records def
rvalue (ade_odgetfield sourceobj x fieldname ycnt) ; get the value (different data types) in the source object
)
(ade_odsetfield destobj x fieldname ycnt rvalue) ; set the value in the destination object record
(setq cnt (1+ cnt)) ; up the count
); end of while (for each occurence)
(setq ycnt (1+ ycnt)) ; up the count
) ; end of while (for each record in the table x)
(setq tablecnt (1+ tablecnt)) ; up the count
) ; end of while (for each attached table name)
)
(progn
)
) ; end of if
(princ) ; clean exit
) ; end of OD_COPY function
The code is build as:
- Get the objects (enames)& a list of all attached tables
- Get the occurence (# records) for each table name and save them in a list of dotted pairs (table . occurrence)
- -While loop 1 (per table in the source object) : from the dotted pair: get the table definition, # columns, set counters
- -Attach x (occurrence) records of the table to the target object
- --While loop 2 : (per occurrence of the table) repeat below for each occurrence of the table
- ---While loop 3 : (per column in the tabledef) : Get the recnum (occurrence nr) from loop 2 - in the source table get the value from table (loop1)/recnum (loop2)/column (loop3) - set the value in the target table/recnum/column
I will also attach a .lsp file with the code and an example .dwg that contains an object with multiple tables each with multiple records.
Solved! Go to Solution.