Author Topic: WCF cann't work in AutoCAD  (Read 1863 times)

0 Members and 1 Guest are viewing this topic.

guohq

  • Newt
  • Posts: 84
WCF cann't work in AutoCAD
« on: December 21, 2016, 10:18:44 AM »
I want to draw a circle in AutoCAD by wcf.

Here is the code  in AutoCAD

  <ServiceContract()> _
    Public Interface IAutoCADService
        <OperationContract()> _
        Function DrawCircle(X As Double, Y As Double, Radius As Double) As String
    End Interface

    Public Class AutoCADPipeService
        Implements IAutoCADService

        <CommandMethod("StartService")> _
        Public Sub StartService()
            Dim Doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Try
                Dim MyAutoCADHost As New ServiceHost(GetType(AutoCADPipeService))
                MyAutoCADHost.AddServiceEndpoint(GetType(IAutoCADService), New NetNamedPipeBinding(), "net.pipe://localhost/AutoCADService")
                MyAutoCADHost.Open()
            Catch ex As System.Exception

            End Try
        End Sub

        Public Function DrawCircle(X As Double, Y As Double, Radius As Double) As String Implements IAutoCADService.DrawCircle
            Dim Doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Try
                Using Doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True)
                    Using Trans As Transaction = Doc.TransactionManager.StartTransactionEx
                        Dim MS As BlockTableRecord = Trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(Doc.Database), OpenMode.ForWrite)
                        Dim Cir As New Circle(New Point3d(X, Y, 0), Vector3d.ZAxis, Radius)
                        DrawCircle = MS.AppendEntity(Cir).Handle.ToString
                        Trans.AddNewlyCreatedDBObject(Cir, True)
                        Trans.Commit()
                    End Using
                End Using
            Catch ex As Exception
                DrawCircle = ""
            End Try
        End Function
    End Class

here is the code  in Demo

Dim ep As New EndpointAddress("net.pipe://localhost/AutoCADService")
            Dim factory As ChannelFactory(Of IAutoCADService) = New ChannelFactory(Of IAutoCADService)(New NetNamedPipeBinding(), ep)
            Dim cad As IAutoCADService = factory.CreateChannel()
            cad.DrawCircle(0, 0, 10)


DrawCircle method doesn't work,why?

n.yuan

  • Bull Frog
  • Posts: 348
Re: WCF cann't work in AutoCAD
« Reply #1 on: December 21, 2016, 10:57:17 AM »
Did you test the end point is accessible? Did you actually debug (say, set a break point on the service side code and the debugging process actually hits the break point? Any error? One the break point is hit in DrawCircle() method, you would be easily tell what went wrong.

In your case, if the call to the service is indeed through and reaches the service, the service host probably does not have access to MdiActiveDocument (but  you should get an exception. That is why I ask you to make sure the service is indeed accessible).

I posted an article quite a while ago on using ASP.NET Web API to automate AutoCAD, which is not too different from using WCF in essence. The discussion/comment on that article may be helpful:

http://drive-cad-with-code.blogspot.ca/2014/01/using-astnet-web-api-to-automate-autocad.html

HTH