Author Topic: Change DrawingUnits within Civil Drawingsettings  (Read 1414 times)

0 Members and 1 Guest are viewing this topic.

ROYNIJKAMP

  • Mosquito
  • Posts: 12
Change DrawingUnits within Civil Drawingsettings
« on: May 04, 2015, 06:10:02 AM »
Hi,

i have my own plugin to check drawings to apply our company standard.
One of the functions must be a check on the DrawingUnits within the Civil Drawingsettings.

I am able to check the current Unit settings, but it fails to change the settings.
What i want is when the current Unit Settings are other then Meters to set the current settings to Meters.

My code:
Code: [Select]
Dim civilDoc As CivilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

        Using trans As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
            Try
                Dim civilUnits As String = civilDoc.Settings.DrawingSettings.UnitZoneSettings.DrawingUnits.ToString
                If Not civilUnits = "Meters" Then
                    ed.WriteMessage("Change UNITS from : " & civilUnits & " to Meters")
                    civilDoc.Settings.DrawingSettings.UnitZoneSettings.DrawingUnits = Settings.DrawingUnitType.Meters
                End If
            Catch ex As Autodesk.Civil.CivilException
                ed.WriteMessage("Exception Message is  : " & ex.Message.ToString())
            Finally
                trans.Commit()
            End Try
        End Using

The above code results in the following error:
Code: [Select]
************** Exception Text **************
System.ArgumentNullException: Value cannot be null.
   at Autodesk.Civil.AttributeHelper.putAttributeGeneric<int>(UInt32 attributeId, IAeccAttributeBin* pAttrBin, Int32 newValue, AeccUserParam* userParam)
   at Autodesk.Civil.AttributeHelper.putAttributeInt(UInt32 attributeId, AcDbObjectId* ObjectId, Int32 newValue)
   at Autodesk.Civil.Settings.SettingsUnitZone.set_DrawingUnits(DrawingUnitType newValue)
   at AnaconToolSet.tabProcessDrawing.checkCivilDrawingSettings()
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Is there somebody who can point me in the right direction?

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Change DrawingUnits within Civil Drawingsettings
« Reply #1 on: May 04, 2015, 09:50:50 AM »
I'm going to venture a guess that the form you are calling this from is either a modeless form or a palette set. In either case you need to Lock the drawing prior to editing the settings. I use C#, but I was able to duplicate the error from a modeless form and solved like so:
Code - C#: [Select]
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             using (DocumentLock lk = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  4.             {
  5.                 CivilDocument civdoc = CivilApplication.ActiveDocument;
  6.                 DrawingUnitType unit = civdoc.Settings.DrawingSettings.UnitZoneSettings.DrawingUnits;
  7.                 if (unit == DrawingUnitType.Feet)
  8.                 {
  9.                     civdoc.Settings.DrawingSettings.UnitZoneSettings.DrawingUnits = DrawingUnitType.Meters;
  10.                 }
  11.             }
  12.         }
  13.