AutoCAD Civil 3D Customization

AutoCAD Civil 3D Customization

Reply
Distinguished Contributor
ebarlevi
Posts: 107
Registered: ‎01-01-2009
Message 1 of 12 (234 Views)
Accepted Solution

VB to C#

234 Views, 11 Replies
10-23-2012 05:10 AM

Hi,

I need to convert the following line from VB to C#.

 

Set Surface = oSurfaces.(sXmlFileName)

 

Idea ?

Thanks

*Expert Elite*
Alfred.NESWADBA
Posts: 8,963
Registered: ‎06-29-2007
Message 2 of 12 (230 Views)

Re: VB to C#

10-23-2012 08:45 AM in reply to: ebarlevi

Hi,

 

look to >>>this page<<< for translation.

 

HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Contributor
ebarlevi
Posts: 107
Registered: ‎01-01-2009
Message 3 of 12 (226 Views)

Re: VB to C#

10-23-2012 09:13 AM in reply to: Alfred.NESWADBA

Alfred hi,

I am familiar with that conversion web. Unfortunately it does not work --)

Thanks

*Expert Elite*
Alfred.NESWADBA
Posts: 8,963
Registered: ‎06-29-2007
Message 4 of 12 (223 Views)

Re: VB to C#

10-23-2012 09:54 AM in reply to: ebarlevi

Hi,

 

>> Unfortunately it does not work

What code do you get as a result, how do the code-lines above (especially the declaration of the var's) look like and what error-message do you get?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Contributor
ebarlevi
Posts: 107
Registered: ‎01-01-2009
Message 5 of 12 (214 Views)

Re: VB to C#

10-23-2012 11:40 PM in reply to: Alfred.NESWADBA

Alfred hi,

First, thank you.

The purpose of the program is to import Surface XML into the current DWG.

The program was supplied by Autodesk in 2011 and was written in VB.

I manage to change some lines but got stacked at the main task which is the import method.

The program looks as follows:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Xml;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.Civil.DatabaseServices.Styles;
using Autodesk.Civil.ApplicationServices;
using CivSuface = Autodesk.Civil.DatabaseServices.Surface;
using OpenFileDialog = System.Windows.Forms.OpenFileDialog;
using DialogResult = System.Windows.Forms.DialogResult;
using Exception = System.Exception;
using oSurfaces = Autodesk.Civil.DatabaseServices.SurfaceOperationAddXmlFile;

namespace ClassLibrary1
{
public class Class1
{

[CommandMethod("ESD_Surface_XML")]
//public void ESD_Surface_XML()
//{
// String surface = gbutton1_Click();
// if (surface == null) return;
//}

public void ESD_Surface_XML()
{
CivilDocument doc = CivilApplication.ActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectIdCollection SurfaceIds = doc.GetSurfaceIds();
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "XML files (*.XML)|*.XML";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
//string path = openFileDialog1.Directory;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)

{
using (myStream)
{

String sXmlFileName = openFileDialog1.FileName;
ed.WriteMessage("\nFile Selected: " + sXmlFileName);


Set Surface = oSurfaces.(sXmlFileName) ;

*Expert Elite*
Alfred.NESWADBA
Posts: 8,963
Registered: ‎06-29-2007
Message 6 of 12 (210 Views)

Re: VB to C#

10-24-2012 12:10 AM in reply to: ebarlevi

Hi,

 

how is "Surface" declared? as Object and you are working with latebinding or as AeccSurface and you have referenced the AECC-lib's?

If that is the complete code-snippet then the base of your problem is that you want to import an XML, but before importing it you try to access the surface (that would exist after import, but not before). Am I right with the statement: when your red line get's executed the surface does not exist in the drawing? If so then the surface-collection has no item called <sXmlFileName>.

 

BTW: when you have the code in VBA, why not translating it to VB.NET then? :smileywink:

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Contributor
ebarlevi
Posts: 107
Registered: ‎01-01-2009
Message 7 of 12 (208 Views)

Re: VB to C#

10-24-2012 12:16 AM in reply to: Alfred.NESWADBA

Alfred,

The program up to the red line was converted from VB to C#.

The red line is still in VB because I do not know how to convert it.

sXmlFileName = string and is the name of the XML file chosen for import.

2013 does not support VBA, and I do not want to install the add on VBA,this is the reason for the whole conversion project.

Eitan

*Expert Elite*
Alfred.NESWADBA
Posts: 8,963
Registered: ‎06-29-2007
Message 8 of 12 (205 Views)

Re: VB to C#

10-24-2012 12:34 AM in reply to: ebarlevi

Hi,

 

>> The program up to the red line was converted from VB to C#

If it's the filename foro import, then my estimation that the surface does not exist in the drawing is correct. So you can't access the surface from a collection of surfaces if it does not exist.

There is some code lost during conversion I think, can you show the original VBA-code?

Is it possible that on top of the sub in VBA the statement

On Error Resume Next

is placed? If so then also VBA would have the problem/the exception, but with the above statement it ignores it. And that makes sense if the guy who wrote the code tried to verify if a surface already existis in the drawing with that name.

 

>> 2013 does not support VBA, and I do not want to install the add on VBA

If you are running on 64bit I understand that. But at the single machine where you do development it might be an advantage to compare the workflow at running/debugging situation.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Contributor
ebarlevi
Posts: 107
Registered: ‎01-01-2009
Message 9 of 12 (203 Views)

Re: VB to C#

10-24-2012 12:47 AM in reply to: Alfred.NESWADBA

Alfred,

This is the original VB from Autodesk (2011) In my C# I change it so I can browse and select an XML file.

'
' Create a surface from the file EG.xml.
'
' NOTE: This uses a hard coded dirercory.
'
Public Function CreateSurfaceByImportXML() As Boolean
Dim oSurfaces As AeccSurfaces
Set oSurfaces = g_oDocument.Surfaces

' Get a reference to the existing surface ("EG"). If
' it does not exist, try loading surface data from a
' XML file.
Dim oSurface As AeccSurface
Dim sXmlFileName As String
On Error Resume Next
Set oSurface = oSurfaces.Item("EG")
On Error GoTo 0

If (oSurface Is Nothing) Then
sXmlFileName = "d:\temp\EG.xml"
Set oSurface = oSurfaces.ImportXML(sXmlFileName)
If (oSurface Is Nothing) Then
Debug.Print "Error loading XML file: " & Err.Description
CreateSurfaceByImportXML = False
Err.Clear
End If
End If

' Fill the screen with the surface, and show the triangles
' that make up the surface.
ThisDrawing.Application.ZoomExtents
Call DisplayBorderTrianglesOnly

CreateSurfaceByImportXML = True
End Function

*Expert Elite*
Alfred.NESWADBA
Posts: 8,963
Registered: ‎06-29-2007
Message 10 of 12 (200 Views)

Re: VB to C#

10-24-2012 01:28 AM in reply to: ebarlevi

Hi,

 

>> On Error Resume Next
>> Set oSurface = oSurfaces.Item("EG")
>> On Error GoTo 0

Ok, it's the "On Error ..." option I mentioned to check if a surface with that name already exists in the drawing.

Now there are two options to continue:

  • scan through all surfaces and verify if any of these have the name, if so, use the "Set ..." statement (is more clean and does not raise an exception)
  • use try catch in the hard way, will raise an exception if no surface with the name exists, but it's less code to write
try 
  {
     oSurface = oSurfaces.Item("EG");
  } 
catch (Exception ex) 
  {
     oSurface = null;
  }

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Announcements
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 or visit the Installation and Licensing Forum to get help installing your software.