Author Topic: Custom AutoCAD Fields  (Read 498 times)

0 Members and 1 Guest are viewing this topic.

jtoverka

  • Newt
  • Posts: 33
Custom AutoCAD Fields
« on: September 25, 2019, 01:05:40 PM »
All,

I use AutoCAD Electrical 2018. I like fields as it reduces manual work and errors. I use fields for the ladder blocks to use the project code. That way, if I update the project code, everything that uses that value will be updated (after refresh).

Here is the string that goes into the text entity: "%<\AcVar CustomDP.Project Code \f "%tc1">%"

One thing to note is the "CustomDP.Project Code"

This value is stored in a block called WD_M in the IEC_PROJ attribute. If I edit this block with a new Project code, the drawing does not get updated upon refresh. However, If I open the drawing properties dialog box it will show the updated project code. When I hit okay (even though I didn't "update" anything) and refresh, the drawing gets updated.

Using the drawing properties dialog box is actually the "proper" way of updating information in the WD_M block. This part is important for my question.

My question is this:
How do I create my own custom variables to use in the AutoCAD fields? This isn't entirely being referenced from another object (WD_M block), not directly anyways. That information is sent somewhere where this custom variable is referencing from. If I delete the WD_M block, the value is still the same code.


Atook

  • Swamp Rat
  • Posts: 942
Re: Custom AutoCAD Fields
« Reply #1 on: September 25, 2019, 04:31:37 PM »
If you purge the block, does the value persist?

If not, you may have to update the block definition.

jtoverka

  • Newt
  • Posts: 33
Re: Custom AutoCAD Fields
« Reply #2 on: September 25, 2019, 07:57:12 PM »
If you purge the block, does the value persist?

Purging the WD_M block does not change the field value.

I am interested in how AutoCAD Electrical gets this value, it has to be stored in a dictionary or something. I just want to be able to create custom values to use in fields. If I can store a value in a dictionary to have it show up as a field option in the dialog box then that would be great.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Custom AutoCAD Fields
« Reply #3 on: September 25, 2019, 11:15:25 PM »
Hi,
Does the UPDATEFIELD command work?
From an API perspective, you can also update field(s) directly.

You can create your own custom field, there’s an example in one of the ARX SDKs, caveat its C++, so you'll need to translate it to .net.
Its not trivial to do, but not crazy hard

jtoverka

  • Newt
  • Posts: 33
Re: Custom AutoCAD Fields
« Reply #4 on: September 26, 2019, 08:48:35 AM »
Hi,
Does the UPDATEFIELD command work?
From an API perspective, you can also update field(s) directly.

You can create your own custom field, there’s an example in one of the ARX SDKs, caveat its C++, so you'll need to translate it to .net.
Its not trivial to do, but not crazy hard


Once I update the WD_M block attribute using properties palette or the enhanced attribute editor dialog box, it does not update the project code stored elsewhere in the drawing. I tried using updateField and it "updates" the block using the old project code. It is only when I open the drawing properties dialog box does it actually update. That dialog box has code stored in it that updates something in the drawing that is tied to the fields.

I just found these
https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-Core/files/GUID-8FCDDD74-0FDC-4189-8407-9EC9D7D482C5-htm.html
https://forums.autodesk.com/t5/net/set-a-custom-drawing-property/td-p/6893092

This answers my question.

jtoverka

  • Newt
  • Posts: 33
Re: Custom AutoCAD Fields
« Reply #5 on: October 31, 2019, 10:38:56 AM »
I have wrote the following as I have found limited information on the web on this topic. The Custom Fields are stored in the SummaryInfo Object which is in the document object. I created functions to read, write and delete these custom fields for the active drawing as well as any document object (for ObjectDBX programs).

Code: [Select]
(defun setCustomField (prop value / acad doc summary i notFound key keyVal readVal)
  (if
    (and
      (= 'STR (type prop))
      (= 'STR (type value))
    )
    (progn
      (vl-load-com)
     
      ; Application -> Document -> SummaryInfo
      (setq acad (vlax-get-acad-object))
      (setq doc (vla-get-activedocument acad))
      (setq summary (vla-get-summaryInfo doc))
     
      ; Read if property exist
      (setq i 0)
      (setq notFound T)
      (while
        (and
          (< i (vla-NumCustomInfo summary))
          notFound
        )
        (vl-catch-all-apply 'vla-getCustomByIndex (list summary i 'key 'keyVal))
        (if (= key prop)
          (setq notFound nil)
        )
        (setq i (1+ i))
      )
     
      ; Modify if property exist
      ; Else, create a new property with value
      (if (not notFound)
        (progn
          (vl-catch-all-apply 'vla-setCustomByKey (list summary prop value))
        )
        (progn
          (vl-catch-all-apply 'vla-AddCustomInfo (list summary prop value))
        )
      )
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
      (if (= readVal value)
        T
      )
    )
  )
)
(defun readCustomField (prop / acad doc summary i notFound key keyVal readVal)
  (if
    (and
      (= 'STR (type prop))
    )
    (progn
      (vl-load-com)
     
      ; Application -> Document -> SummaryInfo
      (setq acad (vlax-get-acad-object))
      (setq doc (vla-get-activedocument acad))
      (setq summary (vla-get-summaryInfo doc))
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
     
      readVal
    )
  )
)
(defun deleteCustomField (prop / acad doc summary readVal)
  (if
    (and
      (= 'STR (type prop))
    )
    (progn
      (vl-load-com)
     
      ; Application -> Document -> SummaryInfo
      (setq acad (vlax-get-acad-object))
      (setq doc (vla-get-activedocument acad))
      (setq summary (vla-get-summaryInfo doc))
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-RemoveCustomByKey (list summary prop))
     
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
      (if (not readVal)
        T
      )
    )
  )
)

; For ObjectDBX Programs
(defun JO:setCustomField (doc prop value / acad doc summary i notFound key keyVal readVal)
  (if
    (and
      (= 'STR (type prop))
      (= 'STR (type value))
    )
    (progn
      ; Application -> Document -> SummaryInfo
      (setq summary (vla-get-summaryInfo doc))
     
      ; Read if property exist
      (setq i 0)
      (setq notFound T)
      (while
        (and
          (< i (vla-NumCustomInfo summary))
          notFound
        )
        (vl-catch-all-apply 'vla-getCustomByIndex (list summary i 'key 'keyVal))
        (if (= key prop)
          (setq notFound nil)
        )
        (setq i (1+ i))
      )
     
      ; Modify if property exist
      ; Else, create a new property with value
      (if (not notFound)
        (progn
          (vl-catch-all-apply 'vla-setCustomByKey (list summary prop value))
        )
        (progn
          (vl-catch-all-apply 'vla-AddCustomInfo (list summary prop value))
        )
      )
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
      (if (= readVal value)
        T
      )
    )
  )
)
(defun JO:readCustomField (doc prop / acad doc summary i notFound key keyVal readVal)
  (if
    (and
      (= 'STR (type prop))
    )
    (progn
      ; Application -> Document -> SummaryInfo
      (setq summary (vla-get-summaryInfo doc))
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
     
      readVal
    )
  )
)
(defun JO:deleteCustomField (doc prop / acad doc summary readVal)
  (if
    (and
      (= 'STR (type prop))
    )
    (progn
      (vl-load-com)
     
      ; Application -> Document -> SummaryInfo
      (setq summary (vla-get-summaryInfo doc))
     
      ; Re-Read to verify updated value
      (vl-catch-all-apply 'vla-RemoveCustomByKey (list summary prop))
     
      (vl-catch-all-apply 'vla-GetCustomByKey (list summary prop 'readVal))
      (if (not readVal)
        T
      )
    )
  )
)

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Custom AutoCAD Fields
« Reply #6 on: October 31, 2019, 11:15:20 AM »
I used to do the same, you can even copy summaryInfo between drawings, or an external file. I used to import data from our estimation software into summaryInfo, I.e. materials and stuff for use in fields. powerful stuff

Google AcDbDatabaseSummaryInfo, acdbGetSummaryInfo, getCustomSummaryInfo, these are in .net too
I'll look and see if I have any old code