Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB to C#

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
ebarlevi
683 Views, 11 Replies

VB to C#

Hi,

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

 

Set Surface = oSurfaces.(sXmlFileName)

 

Idea ?

Thanks

11 REPLIES 11
Message 2 of 12
Alfred.NESWADBA
in reply to: ebarlevi

Hi,

 

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

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 12
ebarlevi
in reply to: Alfred.NESWADBA

Alfred hi,

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

Thanks

Message 4 of 12
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 12
ebarlevi
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) ;

Message 6 of 12
Alfred.NESWADBA
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? 😉

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 12
ebarlevi
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

Message 8 of 12
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 9 of 12
ebarlevi
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

Message 10 of 12
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 11 of 12
ebarlevi
in reply to: Alfred.NESWADBA

Alfred,

Thank you so much for your help.

ebarlevi

Message 12 of 12
andrewpuller3811
in reply to: ebarlevi

Just for reference, the SET is not used in vb.net as it was in vba.

 

try removing the SET statement before attempting conversion, hopefully it will work for you.

 



If a post provides a fix for your issue, click on "Accept as Solution" to help other users find solutions to problems they might have that are similar to yours.

Andrew Puller
Maitland, NSW, Australia
Windows 10 Enterprise 64bit
Intel core i7 11800 @ 2.30 GHz with 32GB Ram
Civil 3d 2021

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report