.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get fields to REGEN on LOAD.

3 REPLIES 3
Reply
Message 1 of 4
LittleJohn20
565 Views, 3 Replies

Get fields to REGEN on LOAD.

Hi all,

I am new so I hope this is the correct location for this post (it involves LISP but it is written in Visual Studio Vb.NET, it has no .lsp associated).  I hope this is a simple answer, but my knowledge of LISP is only what I have read from a dew examples online.  I am using Cad11, VS10 with VB.NET.

 

I have a simple function that breaks down the file name and populates 3 fields with the correct names.  In my template they all get changed correctly, but when I make a new .dwg it takes a few (hopefully unnecessary) steps to get them to change.  The very 1st time opening Cad, the fields do not get recognized ( !fieldname1    returns a nil).  If I close the .dwg and reopen it the fields pull the correct value but do not change in model space  (ex -- !fieldname1 returns 12345, but model space is still showing 54321 in the field).  In order to get the value to change I have to type the Field command in Cad, I don't have to do anything in it, I can immediately close the Field form and then do a regen to get model space to show the correct values.

 

My idea was to hard code in the Field command and hope to be able to close that before the user knows it is even open, but I still don't think that will solve the 1st time Cad is opened problem, and I am having no luck with getting that lisp command to run (like it said, very limited experience with it).  It could just be the syntax is wrong since it is so simple but here is what I thought would work.

 

[code] 

ThisDrawing.SendCommand("field" & vbCr)

 [/code]

I have a thisdrawing...("!fieldname1" & vbcr) that correctly returns the value in the command bar right above this.

 

In the command bar 'field' shows up but the form does not open.  Any suggestions on how to solve this are greatly appreciated!

Thanks for any help, and sorry for the long post but I was trying to give as much info as possible to clarify what I am trying to achieve.

 

LittleJohn

3 REPLIES 3
Message 2 of 4
arcticad
in reply to: LittleJohn20

The fieldeval variable should be set to 31 in each drawing.

This value is set per drawing.

Also this may be from the type of object you are storing the field into. Attributes / Mtext.
Mtext may not update.

If you can post an example it would help
drawing and code

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 3 of 4
LittleJohn20
in reply to: arcticad

Hi arcticad, thank you for the reply.

I went through about 5 of the templates that I have and they all seem to bee 31 when I do a fieldeval.

They are MText, the way I got them is...

1. Typed this routine in cad

 

    (setq "fieldname1" field1)

    (setq "fieldname2" field2)

    (setq "fieldname3" field3)

 

2. Typed 'field' to open the menu

3.LispVariable --> fieldname1,2,3 --> upper

4. Placed them in .dwg and matchprop'ed them to the old MText.

 

My code is very simple...

[code]

If ThisDrawing.FullName.Contains("-") then 'break it apart If (Filename.Length = 3) then ThisDrawing.SendCommand("(setq fieldname1""" ...'filler for breaking the filename string into my sections)) 
ThisDrawing.SendCommand("!fieldname1" & vbcr) 'just a test to see value 
ThisDrawing.SendCommand("field" & vbcr) 'does not open the Field Form End If End If

 [/code] sorry for the way the code is compiled here, i tried to make it half way understandable

 

Before type 'field' / 'regen' ...

 

Before Image 

 

After type..

After Image

 

 

Is this what you were looking for?

Is my method of placing the fields incorrect?

 

LittleJohn

Message 4 of 4
arcticad
in reply to: LittleJohn20

This will check for any fields in the MTEXT and force AutoCAD to evaluate the MTEXT. It will run when you open a file or type in "UF". You will still need to regen.

 

 
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application

Public Class VbExtApp
    Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
    Private DocMan As DocumentCollection

    'Initialize sub
    Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize

        DocMan = AcApp.DocumentManager
        Dim cDoc = AcApp.DocumentManager.MdiActiveDocument
        AddHandler DocMan.DocumentActivated, AddressOf callback_documentActivated

    End Sub

    Private Sub callback_ObjectModified(ByVal sender As Object, ByVal e As ObjectEventArgs)
        updateField()
    End Sub

    '    'Terminate sub
    Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
        RemoveHandler DocMan.DocumentActivated, AddressOf callback_documentActivated
    End Sub

    Private Sub callback_documentActivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
        ' Update Fields In Mtext 
        If e.Document = Nothing Then
            Exit Sub
        Else
            updateField()
        End If

    End Sub

 
    <CommandMethod("uf")> _
    Public Sub updateField()
         Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Using doc.LockDocument
                Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                For Each btrId As ObjectId In bt
                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(btrId, OpenMode.ForRead), BlockTableRecord)
                    For Each objID As ObjectId In btr
                        Dim objEnt As Entity = DirectCast(tr.GetObject(objID, OpenMode.ForRead), Entity)
                        If TypeOf objEnt Is MText Then
                            Dim objMtext As MText = DirectCast(tr.GetObject(objID, OpenMode.ForRead), MText)
                            If objMtext.HasFields Then
                                Dim fieldID As ObjectId = objMtext.GetField
                                Dim field As Field = DirectCast(tr.GetObject(fieldID, OpenMode.ForWrite), Field)
                                field.Evaluate()
                                Dim cEval As String = field.Value
                            End If
                        End If
                    Next
                Next
            End Using
            tr.Commit()
        End Using
     End Sub



End Class

 

---------------------------



(defun botsbuildbots() (botsbuildbots))

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost