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

Problem removing CAD standards programatically (VB.NET)

6 REPLIES 6
Reply
Message 1 of 7
btmsoftware
670 Views, 6 Replies

Problem removing CAD standards programatically (VB.NET)

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

  Try
    ' 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
      Else
        Try
          sPath = HostApplicationServices.Current.FindFile(New FileInfo(tValue.Value.ToString).Name, db, FindFileHint.Default)
        Catch
        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
          record.Erase()
        Else
          ' This is not the one to remove, doing nothing
        End If
      Else
        ' Path not resolved. File not found, nothing to do
      End If
    Next

  Catch ex As System.Exception

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

    Throw ex

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

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

  End Try
End Sub

 

Any help would be greatly appreciated

 

Tags (1)
6 REPLIES 6
Message 2 of 7
fieldguy
in reply to: btmsoftware

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

Message 3 of 7
dgorsman
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.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 7
jeffHGCE
in reply to: dgorsman
Message 5 of 7
btmsoftware
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

 

@Dgorsman

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

Message 6 of 7
btmsoftware
in reply to: btmsoftware

fieldguy,

 

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

Message 7 of 7
fieldguy
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

 

 

 

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