Author Topic: Programmatically Apply Style to Existing Civil 3D Surface  (Read 10390 times)

0 Members and 1 Guest are viewing this topic.

stevenh0616

  • Guest
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #15 on: June 01, 2012, 04:40:01 PM »
Thanks Jeff.

I have done that a few times, and here it is once again. This time I rewrote the code out as you had provided. It doesn't seem to be passing the object over to COM, that's where I'm getting the object not set to instance of an object error.

Below is new code for picking surface. Upon doing the trycast of the style from acad object to COM is where the error occurs.

Code - vb.net: [Select]
  1. Dim SelSurfID As ObjectId = promptForSurface()
  2.         If ObjectId.Null = SelSurfID Then
  3.             'MsgBox appears under prompt for surface
  4.             ' We don't have a surface; we can't continue.
  5.             Return
  6.         End If
  7.  
  8.         Using tr As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
  9.             Dim CivilDoc As CivilDocument = CivilApplication.ActiveDocument
  10.             Dim styles As SurfaceStyleCollection = CivilDoc.Styles.SurfaceStyles
  11.             For Each StyleID As ObjectId In styles
  12.                 Dim style As SurfaceStyle = TryCast(StyleID.GetObject(OpenMode.ForRead), SurfaceStyle)
  13.                 If cbxSurfStyles.Text = style.Name Then
  14.                     MsgBox(style.Name)
  15.                     Dim comStyle As AeccLandLib.AeccSurfaceStyle = TryCast(style.AcadObject, AeccLandLib.AeccSurfaceStyle)
  16.                     MsgBox(comStyle.Name)
  17.                     Dim Surface As ACLB.Surface = TryCast(SelSurfID.GetObject(OpenMode.ForRead), ACLB.Surface)
  18.                     MsgBox(Surface.Name)
  19.                     Dim comsurf As AeccLandLib.AeccSurface = TryCast(Surface.AcadObject, AeccLandLib.AeccSurface)
  20.                     MsgBox(comsurf.Name)
  21.                     comsurf.Style = comStyle
  22.                     tr.Commit()
  23.                 End If
  24.             Next
  25.         End Using

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #16 on: June 01, 2012, 04:49:03 PM »
OK, I think I see where it may be failing. Move the tr.Commit() out of the For Each loop or Exit the loop after you commit. Once you commit(), the Transaction is closed so the next style never gets set.

stevenh0616

  • Guest
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #17 on: June 01, 2012, 04:55:45 PM »
Grrrr.... no go on that either. Still same errror on the trycast from style.acadobject to aeccsurfstyle, line 15.

Here is new code:

Code - vb.net: [Select]
  1. Dim SelSurfID As ObjectId = promptForSurface()
  2.         If ObjectId.Null = SelSurfID Then
  3.             'MsgBox appears under prompt for surface
  4.             ' We don't have a surface; we can't continue.
  5.             Return
  6.         End If
  7.         Dim tr As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
  8.         Using tr
  9.             Dim CivilDoc As CivilDocument = CivilApplication.ActiveDocument
  10.             Dim styles As SurfaceStyleCollection = CivilDoc.Styles.SurfaceStyles
  11.             For Each StyleID As ObjectId In styles
  12.                 Dim style As SurfaceStyle = TryCast(StyleID.GetObject(OpenMode.ForRead), SurfaceStyle)
  13.                 If cbxSurfStyles.Text = style.Name Then
  14.                     MsgBox(style.Name)
  15.                     Dim comStyle As AeccLandLib.AeccSurfaceStyle = TryCast(style.AcadObject, AeccLandLib.AeccSurfaceStyle)
  16.                     MsgBox(comStyle.Name)
  17.                     Dim Surface As ACLB.Surface = TryCast(SelSurfID.GetObject(OpenMode.ForRead), ACLB.Surface)
  18.                     MsgBox(Surface.Name)
  19.                     Dim comsurf As AeccLandLib.AeccSurface = TryCast(Surface.AcadObject, AeccLandLib.AeccSurface)
  20.                     MsgBox(comsurf.Name)
  21.                     comsurf.Style = comStyle
  22.                 End If
  23.             Next
  24.         End Using
  25.         tr.Commit()

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #18 on: June 01, 2012, 05:14:59 PM »
Not sure, Steve, but since I don't have your promptForSurface, or the form with the combobox, I made some adjustments and this works for me:
Code: [Select]

    <CommandMethod("TestSurfStyle")> _
    Public Sub TestSurfStyle()

        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim CivilDoc As CivilDocument = CivilApplication.ActiveDocument
        Using tr As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
            Dim prmpt = New PromptEntityOptions("Select Surface")
            prmpt.SetRejectMessage("Not a surface, try again...")
            prmpt.AddAllowedClass(GetType(ACLB.Surface), False)
            Dim res As PromptEntityResult = doc.Editor.GetEntity(prmpt)
            If res.Status <> PromptStatus.OK Then Return
            Dim SelSurfID As ObjectId = res.ObjectId
            Dim styles As SurfaceStyleCollection = CivilDoc.Styles.SurfaceStyles
            For Each StyleID As ObjectId In styles
                Dim style As SurfaceStyle = TryCast(StyleID.GetObject(OpenMode.ForRead), SurfaceStyle)
                If "Border Only" = style.Name Then
                    MsgBox(style.Name)
                    Dim comStyle As AeccLandLib.AeccSurfaceStyle = TryCast(style.AcadObject, AeccLandLib.AeccSurfaceStyle)
                    MsgBox(comStyle.Name)
                    Dim Surface As ACLB.Surface = TryCast(SelSurfID.GetObject(OpenMode.ForRead), ACLB.Surface)
                    MsgBox(Surface.Name)
                    Dim comsurf As AeccLandLib.AeccSurface = TryCast(Surface.AcadObject, AeccLandLib.AeccSurface)
                    MsgBox(comsurf.Name)
                    comsurf.Style = comStyle
                End If
            Next
            tr.Commit()
        End Using

    End Sub

stevenh0616

  • Guest
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #19 on: June 01, 2012, 05:35:16 PM »
Jeff, you rock!!

That does work!! However there may have been one minor error on my part with reference C3D 2012 COM files but anyways, I'll post a link to the program this weekend once I have it all tweeked.

Thanks so much!! Great news for last minute on a Friday!!

Have a good one!!

stevenh0616

  • Guest
Re: Programmatically Apply Style to Existing Civil 3D Surface
« Reply #20 on: June 03, 2012, 09:33:59 PM »
Thanks again for all your help with making this command work. You can find the command and the VB.net project links on my blog, ENJOY!!   :-D

http://beyondcivil3d.blogspot.com/2012/06/apply-surface-style-to-multiple.html