Active Contributor
Posts: 38
Registered: ‎09-06-2007
Message 1 of 7 (215 Views)

Problem removing CAD standards programatically (VB.NET)

215 Views, 6 Replies
11-21-2013 04:35 AM

Hi there,


Here is the story.


I have a drawing with say 3 CAD standards associated.


Given the full file name (including path), I'm able to retreive the corresponding Xrecord and remove it from the dicstionary.


Then, if I open the configure Standards dialog, I only see the 2 standards left


so far so good.


The problems is that when I close the DWG, saving it of course, then reopen it and open the configure Standards dialog. They're all gone, including the 2 standards that should still be there.

I guess I do something wring but I have no idea what ????????


Here is my code:


Private Sub DetachStandard(_filename as String)
  Dim tr As Transaction = Nothing
  Dim db As Database = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
  Dim sPath As String = ""
  Dim lock As Autodesk.AutoCAD.ApplicationServices.DocumentLock = Nothing

    ' 1st, we lock the database as we're going to modify it
    lock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()

    ' We start the transaction
    tr = db.TransactionManager.StartTransaction

    ' Getting the database dictionary
    Dim nod As DBDictionary = tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite, False)

    ' Getting AcStStandard in the dictionary. If none are present, it will be handled in the catch
    Dim id As ObjectId = nod.Item("AcStStandard")
    ' Getting standards dict
    Dim dict As DBDictionary = tr.GetObject(id, OpenMode.ForWrite, False)
    ' Looping thru standards list

    For Each entry As DictionaryEntry In dict
      sPath = ""

      Dim oId As ObjectId = entry.Value
      Dim record As Xrecord = tr.GetObject(oId, OpenMode.ForWrite, False)

      Dim arrData As Array = record.Data.AsArray
      Dim tValue As TypedValue = arrData(0)

      ' Getting the path of the DWS file
      If File.Exists(tValue.Value) Then
        sPath = tValue.Value.ToString
          sPath = HostApplicationServices.Current.FindFile(New FileInfo(tValue.Value.ToString).Name, db, FindFileHint.Default)
        End Try
      End If

      If Not String.IsNullOrEmpty(sPath) Then
        ' Path has been resolved.

        If String.Compare(sPath, _filename, True) = 0 Then
          ' This is the one to remove
          ' This is not the one to remove, doing nothing
        End If
        ' Path not resolved. File not found, nothing to do
      End If

  Catch ex As System.Exception

    If String.Compare(ex.Message, "eKeyNotFound", True) = 0 Then
      ' No CAD standards in this file
    End If

    Throw ex

    If Not lock Is Nothing Then lock.Dispose() 'Unlocking the database as we're done

      If Not tr Is Nothing Then
        tr.Commit() ' Commitin the transaction
      End If
      ' Ignoring any error when commiting the transaction
    End Try

  End Try
End Sub


Any help would be greatly appreciated


Valued Mentor
Posts: 367
Registered: ‎03-31-2005
Message 2 of 7 (200 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-21-2013 07:18 AM in reply to: btmsoftware

Have you tried moving the commit statement before you dispose the lock? 

*Expert Elite*
Posts: 5,097
Registered: ‎10-12-2006
Message 3 of 7 (182 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-21-2013 11:17 AM in reply to: btmsoftware

Are you certain that removing the XRecord is the *only* thing that happens when standards are removed properly?  There may be other references which are expecting it to be there (via handle or other reference), or an index tracker is expecting them to start at 0 and continue through 1, 2, 3, etc. and not 1, 2, 3 or 0, 2, 3.

If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.

Posts: 3
Registered: ‎03-31-2012
Message 4 of 7 (172 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-21-2013 01:19 PM in reply to: dgorsman
Active Contributor
Posts: 38
Registered: ‎09-06-2007
Message 5 of 7 (139 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-23-2013 12:39 AM in reply to: fieldguy

@ Fieldguy

will try that and let you know
I doubt though since as long as I stay in the drawing, everything is fine, all the other standards are still here, it's only when I close it and re-open it.


@ Jeff

The blog show how to attach a standard (actually that is exactly the code I use to attach those standards), no to detach them



Maybe, but then the question is: What ? Where ? How do I get tid of it ?

Active Contributor
Posts: 38
Registered: ‎09-06-2007
Message 6 of 7 (119 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-24-2013 11:48 PM in reply to: btmsoftware



Moved the transaction commit before unlocking the document and actually, the result is that ALL stamndards are detached immediateely so it's not better because I'm still detaching standards that shouldn't be detached

Valued Mentor
Posts: 367
Registered: ‎03-31-2005
Message 7 of 7 (104 Views)

Re: Problem removing CAD standards programatically (VB.NET)

11-25-2013 07:22 AM in reply to: btmsoftware

I realized that it probably would not solve your problem.  It *looks* like a logic error to me and I decided to mention it.  I am a big fan of "using" statements and the structure is always:

lock document

start transaction

commit transaction

unlock document




You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community