Jump to content

Needing routine that converts table formulated and/or field text to simple table text


RickyD302

Recommended Posts

I'm looking for a routine either lisp, vba, .net that would convert where I've used formulas and fields in a table cell to simple text showing the values but still maintaining the table.

I could always EXPLODE the table but then....I have routines that convert fields in mtext to just text but I can't seem to find any code out there for tables nor am I experienced enough to write my own. TIA

Link to comment
Share on other sites

This should get you on your way:

 

 
(vl-load-com)
(defun c:sample( / ms table height rows columns basePoint insertionPoint cellValue  xOffset yOffset x y)
   (setq ms(vla-get-modelspace(vla-get-activeDocument(vlax-get-acad-object))))
   (setq table (vlax-ename->vla-object(car(entsel "\nSelect table"))))
   (setq height(getint "\nEnter text height"))
   (setq rows(vla-get-rows table))
   (setq columns(vla-get-columns table))
   (setq basePoint (vla-get-insertionPoint table))
   (setq insertionPoint(vlax-make-SafeArray vlax-vbDouble '(0 . 2)))
   (setq yOffset (vlax-safearray-get-element(vlax-variant-value basePoint) 1))
   (setq  y -1)
   (while(<(setq y(1+ y)) rows)
       (setq x -1)
       (setq xOffset (+  (vla-getcolumnWidth table x)(vlax-safearray-get-element(vlax-variant-value basePoint) 0)))
       (setq yOffset(+ (* -1(vla-getrowHeight table y)) yOffset))
     (while (< (setq x (1+ x)) columns)
       (setq cellValue (vla-getText table y x))
           (vlax-safeArray-fill insertionPoint
               (list
                 xOffset
                 yOffset
                   0.0))
     (vla-addText ms cellValue insertionPoint height)
           (setq xOffset(+(vla-getcolumnWidth table x) xOffset))
     )
 )
)

As with practically any post I make on the forum I wouldn't recommend using this code for anything other than a base for understanding how to carry out the process - I'm a big fan of figuring out how actions can be carried out, writing production code for others not som much (If everyone responded to posts with production code no one would bother learning lisp)

 

SOliver

Edited by SOliver
Localisation
Link to comment
Share on other sites

Thanks SOliver I'll check it out.

 

No problem.

 

Don't forget to enter

(vl-load-com)

before running the script. I've added it to the above source now just in case

Link to comment
Share on other sites

Try this VB.Net code, just tested quickly

 
       <CommandMethod("TABB")> _
       Public Sub RemoveFields()
           Dim doc As Document = Application.DocumentManager.MdiActiveDocument
           Dim ed As Editor = doc.Editor
           Dim db As Database = doc.Database
           Dim tvs() As TypedValue = New TypedValue() {New TypedValue(DxfCode.Start, "ACAD_TABLE")}
           Dim opts As PromptSelectionOptions = New PromptSelectionOptions()
           opts.MessageForRemoval = vbNewLine & "Select table(s) only:"
           opts.MessageForAdding = vbNewLine & "Select table(s): "
           Dim filt As SelectionFilter = New SelectionFilter(tvs)
           Dim pso As PromptSelectionResult = ed.GetSelection(opts, filt)
           If Not pso.Status = PromptStatus.OK Then Exit Sub
           If pso.Value.Count = 0 Then Exit Sub
           Using tr As Transaction = db.TransactionManager.StartTransaction
               Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
               ' Dim msg As String = "" ''<--debug only
               For Each sobj As SelectedObject In pso.Value
                   Dim table As Table = CType(tr.GetObject(sobj.ObjectId, OpenMode.ForWrite), Table)
                   For row As Integer = 0 To table.NumRows - 1
                       For col As Integer = 0 To table.NumColumns - 1
                           'msg += (String.Format("Row {0} Col {1}==>{2}{3}", row, col, table.GetContentTypes(row, col), vbNewLine))''<--debug only
                           If table.GetContentTypes(row, col) = CellContentTypes.Field Then
                               Dim fldID As ObjectId = table.FieldId(row, col) 'Field.FromAcadObject()
                               Dim fld As Field = CType(tr.GetObject(fldID, OpenMode.ForRead), Field)
                               Dim strval As String = fld.GetStringValue
                               table.SetCellState(row, col, CellStates.None)
                               fld.UpgradeOpen()
                               fld.Erase()
                               table.SetCellType(row, col, TableCellType.TextCell)
                               table.SetTextString(row, col, strval)
                               table.SetCellState(row, col, CellStates.ContentModifiedAfterUpdate)
                               fld.DowngradeOpen()
                           End If
                       Next
                   Next
               Next
               'ed.WriteMessage(msg)''<--debug only
               tr.Commit()
           End Using
       End Sub

 

 

~'J'~

Link to comment
Share on other sites

Thanks...just need to figure out how to get it into autocad......I have a copy of visual 2005 so I'm ASSuMEing I create a vb project and then copy the code it for starters? Here again links and pointers are appreciated

Link to comment
Share on other sites

Thank you Fixo! that worked....got some interesting warnings when I brought it into visual studio 8.....Man thanks again!

Link to comment
Share on other sites

I could be able to create the project just in Sharpdevelop 3.2

but I'm not sure if this will be working on your end

Glad you fixed it by yourself though

Cheers :)

 

~'J'~

Link to comment
Share on other sites

Yea me too...I've been wanting to medal in vb.net but ....just the time it takes.....I commend you and any others who can just sit down and knock out code!

Link to comment
Share on other sites

This seems complicated, why not just:

 

(defun c:test ( / e i j o s ) (vl-load-com)
 (if
   (and
     (setq e (car (entsel "\nSelect Table: ")))
     (eq "AcDbTable" (vla-get-Objectname (setq o (vlax-ename->vla-object e))))
   )
   (repeat (setq i (vla-get-rows o)) (setq i (1- i))
     (repeat (setq j (vla-get-columns o))
       (setq s (vla-getText o i (setq j (1- j))))
       (vla-deletecontent o i j)
       (vla-settext o i j s)
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

Thanks Lee that worked as well but it took a very long time to run. Windows 7 , 2011 64bit. The .net stuff is so much faster.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...