Jump to content

wblock


metaldan

Recommended Posts

Hi

 

I search the way to make a WBLOCK command in vb.net. I have to ask th user to select some BOM line, with that, ask him to select different object to make wblock with them.

 

 

did someone can help me.

Link to comment
Share on other sites

Save a drawing usual way as ".dwg",

then you can save them after as ".dxf" using Interop Namespace

Or you may want to try this slow method using Reflection:

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Globalization
Imports System.Collections
Imports Autodesk.AutoCAD.Runtime
<Assembly: CommandClass(GetType(OldHorse.ReflectionCommands))> 
Namespace OldHorse
   Public Class ReflectionCommands
       <System.Security.SuppressUnmanagedCodeSecurity()> _
       Public Shared Sub DwgToDxf(acadver As String, fname As String, dxfname As String)
           Dim thisThread As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
           thisThread = New System.Globalization.CultureInfo("en-US")
           Dim appProgID As String = "Autocad.Application" + "." + acadver
           ' get reference on interface  IDispatch
           Dim AcadType As Type = Type.GetTypeFromProgID(appProgID)

           ' run AutoCAD
           Dim AcadApp As Object = Activator.CreateInstance(AcadType)
           Dim visargs() As Object = New Object(0) {}
           visargs(0) = True
           ' make application visible
           AcadApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, AcadApp, visargs, Nothing)
           Dim AcadDocs As Object = AcadApp.GetType().InvokeMember( _
       "Documents", BindingFlags.GetProperty, Nothing, AcadApp, Nothing)
           ' define arguments to open file
           Dim args() As Object = New Object(1) {}
           args(0) = fname
           args(1) = False 'read-only=false, in other words we open file in write mode
           ' try open file
           Dim AcDoc As Object = AcadDocs.GetType.InvokeMember( _
           "Open", BindingFlags.InvokeMethod, Nothing, AcadDocs, args, Nothing)
           Dim Util As Object = New Object
           Try
               ' get active document
               AcDoc = AcadApp.GetType.InvokeMember( _
           "ActiveDocument", BindingFlags.GetProperty, Nothing, AcadApp, Nothing, Nothing)
               'Save document in desired DXF format:
               ''-------------------------------------------------''
               '' DXF formats:
               '' 13 = ac2000_dxf, 25 = ac2004_dxf, 37 = ac2007_dxf
               ''-------------------------------------------------''
               AcDoc.GetType().InvokeMember( _
                    "SaveAs", BindingFlags.InvokeMethod, Nothing, AcDoc, New Object() {dxfname, 37})
               ' Close source document
               'Dim closeargs() As Object = New Object(1) {}
               'closeargs(0) = True
               ' with same name
               '    closeargs(1) = fname
               ' close source document
               ' Try close active document
               '        AcDoc.GetType().InvokeMember( _
               '        "Close", BindingFlags.InvokeMethod, Nothing, AcDoc, closeargs, _
               'Nothing, System.Globalization.CultureInfo.CurrentCulture, Nothing
               ' simplified syntax to close document
               AcDoc.GetType().InvokeMember( _
                       "Close", BindingFlags.InvokeMethod, Nothing, AcDoc, Nothing)
               ' Try quit application
               AcadApp.GetType().InvokeMember( _
               "Quit", BindingFlags.InvokeMethod, Nothing, AcadApp, Nothing)
               MsgBox("Done, check a dxf file")
           Catch ex As System.Exception
               MsgBox("Error: " & ex.Message & vbLf & "Trace: " & ex.StackTrace)
           Finally
               ' garbage clean
               ' release AcDoc.
               releaseObject(AcDoc)
               ' release AcadDocs.
               releaseObject(AcadDocs)
               ' release AcadApp.
               releaseObject(AcadApp)
               ' call garbage cleaner immediatelly
               GC.WaitForPendingFinalizers()
               GC.GetTotalMemory(True)
               GC.WaitForPendingFinalizers()
               GC.GetTotalMemory(True)
               ' restore current culture
               System.Threading.Thread.CurrentThread.CurrentUICulture = thisThread
           End Try
       End Sub
       Public Shared Sub releaseObject(ByVal obj As Object)
           Try
               System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
               obj = Nothing
           Catch ex As System.Exception
               obj = Nothing
           Finally
               GC.Collect()
           End Try
       End Sub
   End Class
End Namespace

 

then in caller class try something like this:

(tested on 2009 only many times)

 
<CommandMethod("SaveAsDxf", "sdxf", CommandFlags.Session Or CommandFlags.Modal)> _
Public Sub TestSaveAsDXF()
Dim acver As System.Version = Autodesk.AutoCAD.ApplicationServices.Application.Version
Dim numver As String = acver.Major.ToString
'' source drawing after executing the WbloneObjects method
Dim fileName As String = "C:\Test\foo.dwg"
'' save document as DXF file
Dim dxfname As String = "C:\Test\foo.dxf"
ReflectionCommands.DwgToDxf(numver, fileName, dxfname)
End Sub

 

~'J'~

Link to comment
Share on other sites

  • 2 weeks later...

Thank's Jeff H

 

 

your DATABASE.DXFOUT method work perfectley.

 

 

 

I put it in a loop to pass all the item in my BOM. For each item, the user select the objet, the vb.net open a new acad document ( temporaly ) , database.dxfout it, and after a close the document.

 

The Dxf is saved perfectly but it don't close it. I have 1 temporaly document for each item.??? wierd. Maybe a put the END USING database at the wrong place.

 

i will check it tomorow.

Link to comment
Share on other sites

Hi I found the way

 

instead of the DOCUMENT.DISPOSE method, I use DOCUMENT.ClLOSEANDDISCARD()

 

THANK'S FOR YOUR HELP

Link to comment
Share on other sites

hi

 

A error apear when a run my progrm on a 64bit machine ( win 7 64bit + acad 201264bit) but not on winxp 32 bit + acad 2008

 

After that line

 

acDbNewDoc.DxfOut("R:\IN BEND\" & Left(DANCART.DESSIN, 8) & "-" & BOML.ITEM & ".dxf", 16, DwgVersion.Current)

i receive this error

INTERNAL ERROE : !dbobji.cpp@7319: eNotOpenForWrite

after, i have a 0 ko dxf file.

Did you have any ideas???

The R:\IN BEND is on a Network, i try to make a manualy saveas DXF at the same location, with the same name and it work.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...