.NET

.NET

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

Problem removing CAD standards programatically (VB.NET)

236 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

  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

 

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 2 of 7 (221 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*
dgorsman
Posts: 5,698
Registered: ‎10-12-2006
Message 3 of 7 (203 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.


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

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

11-21-2013 01:19 PM in reply to: dgorsman
Active Contributor
btmsoftware
Posts: 38
Registered: ‎09-06-2007
Message 5 of 7 (160 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

 

@Dgorsman

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

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

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

11-24-2013 11:48 PM 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

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 7 of 7 (125 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

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.