Author Topic: Help Convert C#.net code to VB.net.  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

MDONUT12

  • Guest
Help Convert C#.net code to VB.net.
« on: March 29, 2007, 11:59:09 AM »
Hi everyone, I received this code from a developer at Autodesk. The code works great in C#.net but when I try and convert it to VB.net which I'm a little more familiar with its basically doing nothing? Either I have totally messed it up or what ever may be the case because I used an Internet translator to convert the code (Ya I know first mistake but a starting point since I know little about C#)

So I was hoping someone could help me.

Thanks for any help!!

Code: [Select]
using System;
using System.Diagnostics;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;

[assembly: ExtensionApplication(typeof(NET.MyApp))]
namespace NET
{
    public class MyApp : IExtensionApplication
    {
public static PublishEvents m_PublishEvents = null;
        public void Terminate()
{
if (m_PublishEvents != null)
{
//m_PublishEvents.Undo();
m_PublishEvents = null;
}
}
        public void Initialize()
{
m_PublishEvents = new PublishEvents();
}
    }

    public class PublishEvents
    {
static private Publisher m_pb = null;
private bool m_bDone;
        public PublishEvents()
        {
m_bDone = false;
m_pb = Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
Do();
        }
public void Do()
{
// Only plant it once.
if(m_bDone == false)
{
m_bDone = true;
}
else
{
return;
}

try
{
m_pb = Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
m_pb.AboutToBeginBackgroundPublishing += new AboutToBeginBackgroundPublishingEventHandler (callback_AboutToBeginBackgroundPublishingEventHandler);

m_pb.AboutToBeginPublishing += new AboutToBeginPublishingEventHandler (callback_AboutToBeginPublishingEventHandler);
m_pb.AboutToEndPublishing += new AboutToEndPublishingEventHandler (callback_AboutToEndPublishingEventHandler);
m_pb.AboutToMoveFile += new AboutToMoveFileEventHandler (callback_AboutToMoveFileHandler);
m_pb.BeginAggregation += new BeginAggregationEventHandler (callback_BeginAggregationEventHandler);
m_pb.BeginEntity += new BeginEntityEventHandler(callback_BeginEntityEventHandler);
m_pb.BeginPublishingSheet += new BeginPublishingSheetEventHandler (callback_BeginPublishingSheetEventHandler);
m_pb.BeginSheet += new BeginSheetEventHandler (callback_BeginSheetEventHandler);
m_pb.CancelledOrFailedPublishing += new CancelledOrFailedPublishingEventHandler(callback_CancelledOrFailedPublishingEventHandler);

m_pb.EndEntity += new EndEntityEventHandler (callback_EndEntityEventHandler);
m_pb.EndPublish +=new EndPublishEventHandler (callback_EndPublishEventHandler);
m_pb.EndSheet +=new EndSheetEventHandler (callback_EndSheetEventHandler);
//m_pb.InitPublishOptionsDialog += new InitPublishOptionsDialogEventHandler (callback_InitPublishOptionsDialogEventHandler);
}
catch (System.Exception ex)
{
MyPrint (string.Format("\terror = {0}",ex.Message));
}
}
public void Undo()
{
if( m_pb == null || m_bDone == false)
return;

try
{
m_pb.AboutToBeginBackgroundPublishing -= new AboutToBeginBackgroundPublishingEventHandler (callback_AboutToBeginBackgroundPublishingEventHandler);

m_pb.AboutToBeginPublishing -= new AboutToBeginPublishingEventHandler (callback_AboutToBeginPublishingEventHandler);
m_pb.AboutToEndPublishing -= new AboutToEndPublishingEventHandler (callback_AboutToEndPublishingEventHandler);
m_pb.AboutToMoveFile -= new AboutToMoveFileEventHandler (callback_AboutToMoveFileHandler);
m_pb.BeginAggregation -= new BeginAggregationEventHandler (callback_BeginAggregationEventHandler);
m_pb.BeginEntity -= new BeginEntityEventHandler(callback_BeginEntityEventHandler);
m_pb.BeginPublishingSheet -= new BeginPublishingSheetEventHandler (callback_BeginPublishingSheetEventHandler);
m_pb.BeginSheet -= new BeginSheetEventHandler (callback_BeginSheetEventHandler);
m_pb.CancelledOrFailedPublishing -= new CancelledOrFailedPublishingEventHandler(callback_CancelledOrFailedPublishingEventHandler);

m_pb.EndEntity -= new EndEntityEventHandler (callback_EndEntityEventHandler);
m_pb.EndPublish -=new EndPublishEventHandler (callback_EndPublishEventHandler);
m_pb.EndSheet -=new EndSheetEventHandler (callback_EndSheetEventHandler);
//m_pb.InitPublishOptionsDialog -+ new InitPublishOptionsDialogEventHandler (callback_InitPublishOptionsDialogEventHandler);

m_pb = null;
}
catch (System.Exception ex)
{
MyPrint (string.Format("\terror = {0}",ex.Message));
}

m_bDone = false;
}
        private void callback_AboutToBeginBackgroundPublishingEventHandler(object sender, AboutToBeginBackgroundPublishingEventArgs e)
        {
            MyPrint("AboutToBeginBackgroundPublishingEventHandler");
            Autodesk.AutoCAD.PlottingServices.DsdData dsdData = e.DsdData;
            MyPrint(string.Format("\tPlotStampOn = {0}", dsdData.PlotStampOn));
        }

private void callback_AboutToBeginPublishingEventHandler (object sender, AboutToBeginPublishingEventArgs e)
{
MyPrint("AboutToBeginPublishingEventHandler");
MyPrint (string.Format("\tget_JobWillPublishInBackground = {0}",e.JobWillPublishInBackground));
Autodesk.AutoCAD.PlottingServices.DsdData  dsdData = e.DsdData;
MyPrint (string.Format("\tPlotStampOn = {0}",dsdData.PlotStampOn));
}
        private void callback_AboutToEndPublishingEventHandler(object sender, PublishEventArgs e)
        {
            MyPrint("AboutToEndPublishingEventHandler");
            MyPrint(string.Format("\tget_DwfFileName = {0}", e.DwfFileName));
            MyPrint(string.Format("\tget_IsMultiSheetDwf = {0}", e.IsMultiSheetDwf));
            MyPrint(string.Format("\tget_TemporaryDwfFileName = {0}", e.TemporaryDwfFileName));
        }

private void callback_AboutToMoveFileHandler(object sender, PublishEventArgs e)
{
MyPrint("AboutToMoveFileHandler");
MyPrint (string.Format("\tget_DwfFileName = {0}",e.DwfFileName));
MyPrint (string.Format("\tget_IsMultiSheetDwf = {0}",e.IsMultiSheetDwf));
MyPrint (string.Format("\tget_TemporaryDwfFileName = {0}",e.TemporaryDwfFileName));
}

private void callback_BeginAggregationEventHandler(object sender, BeginAggregationEventArgs e)
{
MyPrint("BeginAggregationEventHandler");
e.PlotLogger.LogMessage ("From my BeginAggregationEventHandler EVENT!!");
}
private void callback_BeginEntityEventHandler (object sender, PublishEntityEventArgs e)
{
MyPrint("BeginEntityEventHandler");
MyPrint (string.Format("\tget_UniqueEntityId() = {0}",e.UniqueEntityId));
}

private void callback_BeginPublishingSheetEventHandler(object sender, BeginPublishingSheetEventArgs e)
{
MyPrint("BeginPublishingSheetEventHandler");
MyPrint (string.Format("\tDwgName = {0}",e.DsdEntry.DwgName));
e.PlotLogger.LogMessage ("From my BeginPublishingSheetEventHandler EVENT!!");
}

private void callback_BeginSheetEventHandler(object sender, PublishSheetEventArgs e)
{
MyPrint("BeginSheetEventHandler");
MyPrint (string.Format("\tget_CanonicalMediaName = {0}",e.CanonicalMediaName));
MyPrint (string.Format("\tget_Configuration = {0}",e.Configuration));
MyPrint (string.Format("\tget_DrawingScale = {0}",e.DrawingScale.ToString()));
MyPrint (string.Format("\tget_PlotLayoutId = {0}",e.PlotLayoutId.ToString()));
e.PlotLogger.LogMessage ("From my BeginSheetEventHandler EVENT!!");
}
private void callback_CancelledOrFailedPublishingEventHandler(object sender, PublishEventArgs e)
{
MyPrint("CancelledOrFailedPublishingEventHandler");
MyPrint (string.Format("\tget_DwfFileName = {0}",e.DwfFileName));
MyPrint (string.Format("\tget_IsMultiSheetDwf = {0}",e.IsMultiSheetDwf));
MyPrint (string.Format("\tget_TemporaryDwfFileName = {0}",e.TemporaryDwfFileName));
}

private void callback_EndEntityEventHandler (object sender, PublishEntityEventArgs e)
{
MyPrint("EndEntityEventHandler");
MyPrint (string.Format("\tget_UniqueEntityId() = {0}",e.UniqueEntityId));
}

private void callback_EndPublishEventHandler(object sender, PublishEventArgs e)
{
MyPrint("EndPublishEventHandler");
MyPrint (string.Format("\tget_DwfFileName = {0}",e.DwfFileName));
MyPrint (string.Format("\tget_IsMultiSheetDwf = {0}",e.IsMultiSheetDwf));
MyPrint (string.Format("\tget_TemporaryDwfFileName = {0}",e.TemporaryDwfFileName));
}

private void callback_EndSheetEventHandler(object sender, PublishSheetEventArgs e)
{
MyPrint("EndSheetEventHandler");
}

private void MyPrint(String s)
        {
            String strLogFileName = "c:\\test.log";
            System.IO.TextWriter tw= System.IO.File.AppendText(strLogFileName);
            tw.WriteLine(s);
            tw.Close();
        }
    }       
}



MDONUT12

  • Guest
Re: Help Convert C#.net code to VB.net.
« Reply #1 on: March 29, 2007, 12:05:47 PM »
And here is my VB.net code that I cant seem to get to work like it does when i run the c# equivalent above.

Thanks for any help!!

Code: [Select]

Imports System
Imports System.Diagnostics
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Publishing
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.PlottingServices

<Assembly: ExtensionApplication(Type.GetType(NET.MyApp))>

Namespace NET
    Public Class CLASS3
        Implements IExtensionApplication
        Public Shared m_PublishEvents As PublishEvents = Nothing
        Public Sub Terminate()
            If Not m_PublishEvents Is Nothing Then
                'm_PublishEvents.Undo();
                m_PublishEvents = Nothing
            End If
        End Sub
        Public Sub Initialize()
            m_PublishEvents = New PublishEvents()
        End Sub
    End Class

    Public Class PublishEvents
        Private Shared m_pb As Publisher = Nothing
        Private m_bDone As Boolean
        Public Sub New()
            m_bDone = False
            m_pb = Autodesk.AutoCAD.ApplicationServices.Application.Publisher
Do()
        End Sub
Public  Sub Do()
            ' Only plant it once.
            If m_bDone = False Then
                m_bDone = True
            Else
                Return
            End If

            Try
                m_pb = Autodesk.AutoCAD.ApplicationServices.Application.Publisher
                m_pb.AboutToBeginBackgroundPublishing += New AboutToBeginBackgroundPublishingEventHandler(callback_AboutToBeginBackgroundPublishingEventHandler)

                m_pb.AboutToBeginPublishing += New AboutToBeginPublishingEventHandler(callback_AboutToBeginPublishingEventHandler)
                m_pb.AboutToEndPublishing += New AboutToEndPublishingEventHandler(callback_AboutToEndPublishingEventHandler)
                m_pb.AboutToMoveFile += New AboutToMoveFileEventHandler(callback_AboutToMoveFileHandler)
                m_pb.BeginAggregation += New BeginAggregationEventHandler(callback_BeginAggregationEventHandler)
                m_pb.BeginEntity += New BeginEntityEventHandler(callback_BeginEntityEventHandler)
                m_pb.BeginPublishingSheet += New BeginPublishingSheetEventHandler(callback_BeginPublishingSheetEventHandler)
                m_pb.BeginSheet += New BeginSheetEventHandler(callback_BeginSheetEventHandler)
                m_pb.CancelledOrFailedPublishing += New CancelledOrFailedPublishingEventHandler(callback_CancelledOrFailedPublishingEventHandler)

                m_pb.EndEntity += New EndEntityEventHandler(callback_EndEntityEventHandler)
                m_pb.EndPublish += New EndPublishEventHandler(callback_EndPublishEventHandler)
                m_pb.EndSheet += New EndSheetEventHandler(callback_EndSheetEventHandler)
                'm_pb.InitPublishOptionsDialog += new InitPublishOptionsDialogEventHandler (callback_InitPublishOptionsDialogEventHandler);
            Catch ex As System.Exception
                MyPrint(String.Format("\terror = {0}", ex.Message))
            End Try
        End Sub
        Public Sub Undo()
            If m_pb = Nothing Or m_bDone = False Then
                Return
            End If

            Try
Dim - As m_pb.AboutToBeginBackgroundPublishing =  New AboutToBeginBackgroundPublishingEventHandler(callback_AboutToBeginBackgroundPublishingEventHandler)

Dim - As m_pb.AboutToBeginPublishing =  New AboutToBeginPublishingEventHandler(callback_AboutToBeginPublishingEventHandler)
Dim - As m_pb.AboutToEndPublishing =  New AboutToEndPublishingEventHandler(callback_AboutToEndPublishingEventHandler)
Dim - As m_pb.AboutToMoveFile =  New AboutToMoveFileEventHandler(callback_AboutToMoveFileHandler)
Dim - As m_pb.BeginAggregation =  New BeginAggregationEventHandler(callback_BeginAggregationEventHandler)
Dim - As m_pb.BeginEntity =  New BeginEntityEventHandler(callback_BeginEntityEventHandler)
Dim - As m_pb.BeginPublishingSheet =  New BeginPublishingSheetEventHandler(callback_BeginPublishingSheetEventHandler)
Dim - As m_pb.BeginSheet =  New BeginSheetEventHandler(callback_BeginSheetEventHandler)
Dim - As m_pb.CancelledOrFailedPublishing =  New CancelledOrFailedPublishingEventHandler(callback_CancelledOrFailedPublishingEventHandler)

Dim - As m_pb.EndEntity =  New EndEntityEventHandler(callback_EndEntityEventHandler)
Dim - As m_pb.EndPublish = New EndPublishEventHandler(callback_EndPublishEventHandler)
Dim - As m_pb.EndSheet = New EndSheetEventHandler(callback_EndSheetEventHandler)
                'm_pb.InitPublishOptionsDialog -+ new InitPublishOptionsDialogEventHandler (callback_InitPublishOptionsDialogEventHandler);

                m_pb = Nothing
            Catch ex As System.Exception
                MyPrint(String.Format("\terror = {0}", ex.Message))
            End Try

            m_bDone = False
        End Sub
        Private Sub callback_AboutToBeginBackgroundPublishingEventHandler(ByVal sender As Object, ByVal e As AboutToBeginBackgroundPublishingEventArgs)
            MyPrint("AboutToBeginBackgroundPublishingEventHandler")
            Dim dsdData As Autodesk.AutoCAD.PlottingServices.DsdData = e.DsdData
            MyPrint(String.Format("\tPlotStampOn = {0}", dsdData.PlotStampOn))
        End Sub

        Private Sub callback_AboutToBeginPublishingEventHandler(ByVal sender As Object, ByVal e As AboutToBeginPublishingEventArgs)
            MyPrint("AboutToBeginPublishingEventHandler")
            MyPrint(String.Format("\tget_JobWillPublishInBackground = {0}", e.JobWillPublishInBackground))
            Dim dsdData As Autodesk.AutoCAD.PlottingServices.DsdData = e.DsdData
            MyPrint(String.Format("\tPlotStampOn = {0}", dsdData.PlotStampOn))
        End Sub
        Private Sub callback_AboutToEndPublishingEventHandler(ByVal sender As Object, ByVal e As PublishEventArgs)
            MyPrint("AboutToEndPublishingEventHandler")
            MyPrint(String.Format("\tget_DwfFileName = {0}", e.DwfFileName))
            MyPrint(String.Format("\tget_IsMultiSheetDwf = {0}", e.IsMultiSheetDwf))
            MyPrint(String.Format("\tget_TemporaryDwfFileName = {0}", e.TemporaryDwfFileName))
        End Sub

        Private Sub callback_AboutToMoveFileHandler(ByVal sender As Object, ByVal e As PublishEventArgs)
            MyPrint("AboutToMoveFileHandler")
            MyPrint(String.Format("\tget_DwfFileName = {0}", e.DwfFileName))
            MyPrint(String.Format("\tget_IsMultiSheetDwf = {0}", e.IsMultiSheetDwf))
            MyPrint(String.Format("\tget_TemporaryDwfFileName = {0}", e.TemporaryDwfFileName))
        End Sub

        Private Sub callback_BeginAggregationEventHandler(ByVal sender As Object, ByVal e As BeginAggregationEventArgs)
            MyPrint("BeginAggregationEventHandler")
            e.PlotLogger.LogMessage("From my BeginAggregationEventHandler EVENT!!")
        End Sub
        Private Sub callback_BeginEntityEventHandler(ByVal sender As Object, ByVal e As PublishEntityEventArgs)
            MyPrint("BeginEntityEventHandler")
            MyPrint(String.Format("\tget_UniqueEntityId() = {0}", e.UniqueEntityId))
        End Sub

        Private Sub callback_BeginPublishingSheetEventHandler(ByVal sender As Object, ByVal e As BeginPublishingSheetEventArgs)
            MyPrint("BeginPublishingSheetEventHandler")
            MyPrint(String.Format("\tDwgName = {0}", e.DsdEntry.DwgName))
            e.PlotLogger.LogMessage("From my BeginPublishingSheetEventHandler EVENT!!")
        End Sub

        Private Sub callback_BeginSheetEventHandler(ByVal sender As Object, ByVal e As PublishSheetEventArgs)
            MyPrint("BeginSheetEventHandler")
            MyPrint(String.Format("\tget_CanonicalMediaName = {0}", e.CanonicalMediaName))
            MyPrint(String.Format("\tget_Configuration = {0}", e.Configuration))
            MyPrint(String.Format("\tget_DrawingScale = {0}", e.DrawingScale.ToString()))
            MyPrint(String.Format("\tget_PlotLayoutId = {0}", e.PlotLayoutId.ToString()))
            e.PlotLogger.LogMessage("From my BeginSheetEventHandler EVENT!!")
        End Sub
        Private Sub callback_CancelledOrFailedPublishingEventHandler(ByVal sender As Object, ByVal e As PublishEventArgs)
            MyPrint("CancelledOrFailedPublishingEventHandler")
            MyPrint(String.Format("\tget_DwfFileName = {0}", e.DwfFileName))
            MyPrint(String.Format("\tget_IsMultiSheetDwf = {0}", e.IsMultiSheetDwf))
            MyPrint(String.Format("\tget_TemporaryDwfFileName = {0}", e.TemporaryDwfFileName))
        End Sub

        Private Sub callback_EndEntityEventHandler(ByVal sender As Object, ByVal e As PublishEntityEventArgs)
            MyPrint("EndEntityEventHandler")
            MyPrint(String.Format("\tget_UniqueEntityId() = {0}", e.UniqueEntityId))
        End Sub

        Private Sub callback_EndPublishEventHandler(ByVal sender As Object, ByVal e As PublishEventArgs)
            MyPrint("EndPublishEventHandler")
            MyPrint(String.Format("\tget_DwfFileName = {0}", e.DwfFileName))
            MyPrint(String.Format("\tget_IsMultiSheetDwf = {0}", e.IsMultiSheetDwf))
            MyPrint(String.Format("\tget_TemporaryDwfFileName = {0}", e.TemporaryDwfFileName))
        End Sub

        Private Sub callback_EndSheetEventHandler(ByVal sender As Object, ByVal e As PublishSheetEventArgs)
            MyPrint("EndSheetEventHandler")
        End Sub

        Private Sub MyPrint(ByVal s As String)
            Dim strLogFileName As String = "c:\\test.log"
            Dim tw As System.IO.TextWriter = System.IO.File.AppendText(strLogFileName)
            tw.WriteLine(s)
            tw.Close()
        End Sub
    End Class
End Namespace

MDONUT12

  • Guest
Re: Help Convert C#.net code to VB.net.
« Reply #2 on: March 29, 2007, 01:25:58 PM »
Possibly here is a more simple solution. I've found that I guess you can call DLL files from inside other languages?

So can I simply just call in this other dll I made in VB.net from inside this C# code? Down where I put in mydll? However I think i might have the coding wrong as its giving me errors. Mydll.Form1.ActiveForm.Show;

Thanks,

Code: [Select]
    private void callback_AboutToBeginPublishingEventHandler (object sender, AboutToBeginPublishingEventArgs e)
{
MyPrint("AboutToBeginPublishingEventHandler");
MyPrint (string.Format("\tget_JobWillPublishInBackground = {0}",e.JobWillPublishInBackground));
Autodesk.AutoCAD.PlottingServices.DsdData  dsdData = e.DsdData;
MyPrint (string.Format("\tPlotStampOn = {0}",dsdData.PlotStampOn));

Mydll.Form1.ActiveForm.Show;

}



Nathan Taylor

  • Guest
Re: Help Convert C#.net code to VB.net.
« Reply #3 on: March 29, 2007, 06:26:53 PM »
The problem is the translator not converting the adding and removal of the event handlers properly. I will have a look at correcting it soon.

Regards - Nathan

MDONUT12

  • Guest
Re: Help Convert C#.net code to VB.net.
« Reply #4 on: March 29, 2007, 06:55:37 PM »
Thanks Nathan!

Nathan Taylor

  • Guest
Re: Help Convert C#.net code to VB.net.
« Reply #5 on: March 29, 2007, 07:15:36 PM »
Sample AddHandler correction:
Code: [Select]
m_pb.AboutToBeginBackgroundPublishing += New AboutToBeginBackgroundPublishingEventHandler(callback_AboutToBeginBackgroundPublishingEventHandler)
to
Code: [Select]
AddHandler m_pb.AboutToBeginBackgroundPublishing, New AboutToBeginBackgroundPublishingEventHandler(AddressOf callback_AboutToBeginBackgroundPublishingEventHandler)

Sample RemoveHandler correction:
Code: [Select]
Dim - As m_pb.AboutToBeginBackgroundPublishing =  New AboutToBeginBackgroundPublishingEventHandler(callback_AboutToBeginBackgroundPublishingEventHandler)
to
Code: [Select]
RemoveHandler m_pb.AboutToBeginBackgroundPublishing, New AboutToBeginBackgroundPublishingEventHandler(AddressOf callback_AboutToBeginBackgroundPublishingEventHandler)

Regards - Nathan