@danielFMN5S ,
@danielFMN5S wrote:
I want to ask if it is possible to put OD in a regular line or polyline that currently does not have OD?
^^^^^ Yes, it is possible.
Here's an example of the code that uses the custom functions I made to add custom Object Data to an entity:
;; Creates an Object Data (OD) table & adds custom OD to an entity
(defun c:TEST ( / ODTableName ODTableDesc ODTableFields MyCustomOD ent)
(setq ODTableName "My_Custom_OD_Table")
(setq ODTableDesc "This is the table description.")
(setq ODTableFields
'(
("Field_1" "The description for Field 1." "character" "")
("Field_2" "The description for Field 2." "integer" 0)
("Field_3" "The description for Field 3." "point" "0,0,0")
("Field_4" "The description for Field 4." "real" 0.0)
)
);setq
(if (not (OD-CreateTable ODTableName ODTableDesc ODTableFields))
(progn (alert (princ "OD Table not created, check command history.")) (exit))
);if
(setq MyCustomOD
'(
("Field_1" . "My custom text")
("Field_2" . 123)
("Field_3" . "123.00,456.00,789.00")
("Field_4" . 321.123)
)
);setq
(if (setq ent (car (entsel "\nSelect an Entity to add OD to: ")))
(if (OD-AddToEntity ent ODTableName MyCustomOD)
(alert (princ "\nCustom Object Data was added successfully!"))
;else
(alert (princ "\nFailed to add custom Object Data, check command history."))
);if
;else
(alert (princ "\nNo entity was selected."))
);if
(princ)
);defun
Here's the 2 custom functions you will need (they're so long because they do a LOT of checking if you're not familiar with OD):
;; Creates an Object Data (OD) table w/ user-specified fields
;; tblName - string, the name of the OD table
;; tblDesc - string, the description of the OD table
;; lstFields - list, of fields for the OD table, and all necessary default info to populate them
;; returns - string, of OD table created, else nil if unsuccessful
;; 'lstFields' Format:
;; (([field_name] [field_description] [field_type{character,integer,point,real}] [default_value]) (...) ...)
;; 'lstFields' Example:
;; (("Street_Name" "" "character" "") ("SL" "The Speed Limit of the Street." "integer" 25)))
;; Notes:
;; - All 4 field properties must be provided: [field_name],[field_description],[field_type],[default_value]
;; - 4 field properties are of respective type: string,string,string{only: "character","integer","point","real"},{either: string,integer,string [formatted like: "X,Y,Z"],real}
;; - Empty Table Description [double quotes ""] is allowed
;; - Empty Field Descriptions [double quotes ""] are allowed
;; - Default values must be provided [worst case, use: "" | 0 | "0,0,0" | 0.0 ...respectively]
(defun OD-CreateTable (tblName tblDesc lstFields / badChars badCharList errMsg colTypes tblDef)
(setq badChars "!@#$%^&* (){}[];:',.<>?=+/\"\\")
(setq badCharList (vl-string->list badChars))
(setq errMsg
(cond
((not (eq 'STR (type tblName))) "\nTable Name not a string.")
((apply 'or (mapcar '(lambda (c) (member c badCharList)) (vl-string->list tblName)))
(strcat "\nTable Name contains an invalid character: " badChars)
)
((> (strlen tblName) 25) "\nTable names are limited to 25 characters.")
((member (strcase tblName) (mapcar 'strcase (ade_odtablelist)))
(strcat "\nThe table '" tblName "' already exists.")
)
((not (eq 'STR (type tblDesc))) "\nTable Description not a string.")
((not (listp lstFields)) "\nFields List was not a list.")
((not (apply 'and
(mapcar '(lambda (colNameType) (eq 'STR colNameType))
(mapcar 'type
(mapcar 'car lstFields)
);mapcar
);mapcar
);apply
);not
"\nOne or more Field Column Names was not a string."
)
((apply 'or
(mapcar
'(lambda (colName)
(apply 'or (mapcar '(lambda (c) (member c badCharList)) (vl-string->list colName)))
);lambda
(mapcar 'car lstFields)
);mapcar
);apply
(strcat "\nOne or more Field Column Names contain invalid characters: " badChars)
)
((apply 'or
(mapcar
'(lambda (colName) (> (strlen colName) 31))
(mapcar 'car lstFields)
);mapcar
);apply
"\nOne or more Field Column Names was longer than the allotted 31 character limit."
)
((not (apply 'and
(mapcar '(lambda (colDescType) (eq 'STR colDescType))
(mapcar 'type
(mapcar 'cadr lstFields)
);mapcar
);mapcar
);apply
);not
"\nOne or more Field Column Descriptions was not a string."
)
((not (apply 'and
(mapcar '(lambda (colType) (eq 'STR colType))
(mapcar 'type
(mapcar 'caddr lstFields)
);mapcar
);mapcar
);apply
);not
"\nOne or more Field Column Types was not a string."
)
((not (apply 'and
(mapcar
'(lambda (colType)
(setq colTypes (cons (setq colType (strcase colType t)) colTypes))
(member colType '("character" "integer" "point" "real"))
);lambda
(mapcar 'caddr lstFields)
);mapcar
);apply
);not
"\nOne or more Field Column Types was not an appropriate value [\"character\",\"integer\",\"point\",\"real\"]."
)
((not (apply 'and
(mapcar
'(lambda (colType colVal / typ pos)
(setq typ (type colVal))
(cond
((eq "character" colType) (eq 'STR typ))
((eq "integer" colType) (eq 'INT typ))
((eq "point" colType)
(and
(eq 'STR typ)
(setq pos (vl-string-search "," colVal))
(vl-string-search "," colVal (1+ pos))
);and
)
((eq "real" colType) (eq 'REAL typ))
);cond
);lambda
(reverse colTypes)
(mapcar 'cadddr lstFields)
);mapcar
);apply
);not
"\nOne or more Field Column Default Values did not correspond to the assigned Type."
)
);cond
);setq
(if (not errMsg)
(progn
(setq tblDef
(append
(list (cons "tablename" tblName) (cons "tabledesc" tblDesc))
(list (cons "columns"
(mapcar
'(lambda (field)
(list
(cons "colname" (car field))
(cons "coldesc" (cadr field))
(cons "coltype" (caddr field))
(cons "defaultval" (cadddr field))
);list
);lambda
lstFields
);mapcar
));list/cons
);append
);setq
(if (ade_oddefinetab tblDef)
tblName
);if
);progn
;else
(progn (prompt (strcat "The following Error ocurred when creating the OD table:\n" errMsg)) nil)
);if
);defun
;; Adds Object Data (OD) to a specified entity
;; ent - ename, of entity to add Object Data to
;; tbl - string, of OD table name that properties belong to [Must Already Exist!]
;; props - list, of ((prop . val) (prop . val) ...) dotted-pair properties & values to add to entity as Object Data
;; returns - t if successful, else nil
;; Notes:
;; - This function will always override the existing values of any object data already attached to the entity under the provided table name [tbl]
(defun OD-AddToEntity (ent tbl props / errMsg tblDef tblFields cnt)
(setq errMsg
(cond
((not (member (strcase tbl) (mapcar 'strcase (ade_odtablelist))))
(strcat "\nThe OD table '" tbl "' does not exist.")
)
((not (apply 'and (mapcar '(lambda (p) (eq 'STR (type p))) (mapcar 'car props))))
"\nOne or more provided property names was not a string."
)
((not (setq tblDef (ade_odtabledefn tbl))) "\nCould not retrieve Table Definition.")
((not (setq tblFields
(mapcar
'(lambda (f) (cons (strcase (cdr (assoc "ColName" f))) (strcase (cdr (assoc "ColType" f)) t)))
(cdr (assoc "Columns" tblDef))
);mapcar
);setq
);not
"\nCould not retrieve Table Fields (columns)."
)
((not (apply 'and
(mapcar
'(lambda (p)
(member (strcase p) (mapcar 'car tblFields))
);lambda
(mapcar 'car props)
);mapcar
);apply
);not
"\nOne or more of the provided property names was not a field (column) in the OD table."
)
((not (apply 'and
(mapcar
'(lambda (p / colType pos)
(setq colType (cdr (assoc (strcase (car p)) tblFields)))
(cond
((eq "character" coltype) (eq 'STR (type (cdr p))))
((eq "integer" coltype) (eq 'INT (type (cdr p))))
((eq "point" coltype)
(and
(eq 'STR (type (cdr p)))
(setq pos (vl-string-search "," (cdr p)))
(vl-string-search "," (cdr p) (1+ pos))
);and
)
((eq "real" coltype) (eq 'REAL (type (cdr p))))
);cond
);lambda
props
);mapcar
);apply
);not
"\nOne or more of the provided property values does not match the Type (character,integer,point,real) allotted for the field (column)."
)
);cond
);setq
(if (not errMsg)
(progn
(setq cnt (ade_odrecordqty ent tbl))
(if (zerop cnt) (ade_odaddrecord ent tbl))
(foreach p props
(ade_odsetfield ent tbl (car p) 0 (cdr p))
);foreach
t
);progn
;else
(progn (prompt (strcat "The following Error ocurred when trying to add OD to the entity:\n" errMsg)) nil)
);if
);defun
Best,