.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selecting then modifing results from import command, interop

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
cyconnely
742 Views, 2 Replies

Selecting then modifing results from import command, interop

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Interop
Imports System.Object
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.Internal.Forms
Imports System
Imports System.IO
Imports System.Runtime.InteropServices


Namespace TestSelecting
    Public Class MyCommands

        <CommandMethod("Program")> Public Sub Program()
            '' Get the current document editor
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database
            Dim acDocEd As Editor = acDoc.Editor
            Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
            Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument


            AcadApp = CType(GetObject(, "AutoCAD.Application.18.2"), Autodesk.AutoCAD.Interop.AcadApplication)
            '' Start a transaction
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                Dim ssetObj As AcadSelectionSet

                '' Request for objects to be selected in the drawing area
                Dim acSSPrompt As PromptSelectionResult = acDocEd.GetSelection(acSelFtr)

                '' If the prompt status is OK, objects were selected
                If acSSPrompt.Status = PromptStatus.OK Then
                    Dim acSSet As SelectionSet = acSSPrompt.Value

                    '' Step through the objects in the selection set
                    For Each acSSObj As SelectedObject In acSSet

                        '' Check to make sure a valid SelectedObject object was returned
                        If Not IsDBNull(acSSObj) Then
                            '' Open the selected object for write
                            Dim acEnt As Entity = CType(acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite), Entity)
                            If Not IsDBNull(acEnt) Then
                                acadDoc = AcadApp.ActiveDocument
                               

                                Dim point(0 To 2) As Double
                                
                                point(0) = 2.0
                                point(1) = 2.0
                                point(2) = 0.0

                                Dim afterWMFObj As Object
                                afterWMFObj = acadDoc.Import("C:\Users\C\Documents\Drawing1.wmf", point, 2.0)
                                
                                
                                Dim newEntity As Entity = CType((acTrans.GetObject(CType(afterWMFObj, ObjectId), OpenMode.ForWrite)), Entity)

                                newEntity.ColorIndex = 3


                            End If
                        End If

                    Next

                    '' Save the new object to the database
                    acTrans.Commit()
                End If

                '' Dispose of the transaction
            End Using
        End Sub



    End Class
End Namespace

 

************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
   at TestSelectingFiltering.TestSelecting.MyCommands.FilterBlueCircleOnLayer0()
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

 I'm trying to be able to add the results from the imported WMF into an entity, so that I'll be able to modify it later.

I keep getting this error. In this code I'm trying to change the imported WMF drawing to a green color.

 

Any help would be Awesome! -Thanks

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: cyconnely

Have you debugged by stepping through the code line-by-line? Instead of posting a big chunk of code and exception message at the end, it would be nicer to people who may help you to do your due dilligent work to pointing out which line of the code actually causes the exception.

 

There are a few things regarding your code:

 

1. It is not necessary to use VB's GetObject() to get an COM AutoCAD application object. It is especially not good to tie the code with specific version of AutoCAD by using "AutoCAD.Application.18.2", becuse your code is .NET API code that runs inside AutoCAD, whatever version it is would be irrelevent. You obtain the COM AcadApplication by:

 

AcadApp=CType(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, AcadApplication)

 

As matter of fact, you do not need an AcadApplication object in this importing process. What you need is AcadDocument from current MdiActiveDocument:

 

Dim acadDoc As AcadDocument=CType(Application.DocumentManager.MdiActiveDocument.AcadDocument, AcadDocument)

 

2. I do not see why asking user to select entity or entities and has anything to do with importing WMF. Or maybe you just omitted something irrelevant with this exception, if so, forget my questioning on this.

 

3. From my observation of the code, the exception is after the importing is completed and when you attempt to obtain the imported entity:

 

Dim newEntity As Entity = CType((acTrans.GetObject(CType(afterWMFObj, ObjectId), OpenMode.ForWrite)), Entity)

 

Do you know what type of entity is created when an WMF file is imported into AutoCAD? It is definitely not an ObjectId in Autodesk.AutoCAD.DatabaseService namespace, thus CType(afterWMFObj, ObjectId) fails.

 

According to AutoCAD VBA help document, WMF file is imported into AutoCAD as AcadBlockreference (an COM object). So, after imported into AutoCAD in .NET API code, you need to obtain an .NET API object froom the COM object. The code would be like:

 

Dim afterWMFObj As Object
afterWMFObj = acadDoc.Import("C:\Users\C\Documents\Drawing1.wmf",....)

 

Dim objId As ObjectId = Entity.FromAcadObject(afterWMFObj)

Dim newEnt As Entity=acTrans.GetObject(objId,OpenMode.ForWrite)

....

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
cyconnely
in reply to: norman.yuan

Thank you so much!

You were right about the AcadApp, I guess that's what I get for using code scavenged on the internet... the other fragments of code were from some other things that I took out. And your objId code worked great! Thanks again, And I don't know why I never thought to look at the vba reference for the import command... duh.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost