Rob S Posted February 7, 2012 Share Posted February 7, 2012 Hi, I want to add a block with vb.net in autocad in the middle of a line. The line also has to be drawn so I make the user click on the start and end point of the line. I calculate from those points the midpoint. Then there is a problem to add the block on the calculated midpoint. [font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] startpunt [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Object [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] eindpunt [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Object [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] midden [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Object [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] inspectieput [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadObject [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] putnummer [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadObject [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] dekselhoogte [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadObject [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] aanlegjaar [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadBlock [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] strenginfo [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadBlock [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] streng [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af][font=Consolas][size=2][color=#2b91af]AcadObject [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] attributen [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Object [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] I [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Integer [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Dim[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] attribuutnaam [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]As[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]String [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#008000][font=Consolas][size=2][color=#008000][font=Consolas][size=2][color=#008000]' Get the first point without entering a base point [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]startpunt = ThisDrawing.Utility.GetPoint(, [/size][/font][/size][/font][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515]"What is the beginning of the line?: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]) eindpunt = ThisDrawing.Utility.GetPoint(, [/size][/font][/size][/font][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515]"What is the end of the line?: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]) midden(0) = (startpunt(0) + eindpunt(0)) / 2 midden(1) = startpunt(1) / 2 + eindpunt(1) / 2 midden(2) = startpunt(2) / 2 + eindpunt(2) / 2 [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]If[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] CBXbestaandstreng.Checked = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]True[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Then [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Else [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]streng = ThisDrawing.ModelSpace.AddLine(startpunt, eindpunt) aanlegjaar = ThisDrawing.ModelSpace.InsertBlock(midden, [/size][/font][/size][/font][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515][font=Consolas][size=2][color=#a31515]"C:\block 1.dwg"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2], 1, 1, 1, 0)[/size][/font][/size][/font] [font=Consolas][size=2][font=Consolas][size=2]end if [/size][/font][/size][/font][font=Consolas][size=2][font=Consolas][size=2][/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Next [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]I = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Nothing [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]attributen = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Nothing [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2]attribuutnaam = [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]Nothing [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]End[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Consolas][size=2][font=Consolas][size=2] [/size][/font][/size][/font][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff]If[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff][font=Consolas][size=2][color=#0000ff] [/color][/size][/font][/color][/size][/font][/color][/size][/font] Anyone ideas? Quote Link to comment Share on other sites More sharing options...
SEANT Posted February 8, 2012 Share Posted February 8, 2012 This resembles VBA more than VB.NET. How is this code snippet written, in the VBA editor? Visual Studio? Will it be part of a DLL, loaded via AutoCAD’s NETLOAD command? Or, perhaps, made as an EXE, interfacing with AutoCAD via Autodesk.AutoCAD.Interop? The best advice we could give is based on how we expect the code to be compiled. Quote Link to comment Share on other sites More sharing options...
Rob S Posted February 8, 2012 Author Share Posted February 8, 2012 Thanks for your answer. It is written in Visual Studio. With the netload command you load the DLL and after that you can use this command. Quote Link to comment Share on other sites More sharing options...
SEANT Posted February 8, 2012 Share Posted February 8, 2012 Unfortunately, I don’t have a lot of experience using Interop - but, what if you changed this line to: Dim aanlegjaar As AcadBlockReference Presumably, this project is a VBA to VB.NET migration. If it is, this would certainly be the most direct translation. The .NET API has additional capabilities for this (and many other) tasks. Time is well spent investigating pure .NET (Managed ARX) if the code is destined to run via netload. Quote Link to comment Share on other sites More sharing options...
Rob S Posted February 9, 2012 Author Share Posted February 9, 2012 Hi thanks for your reaction. Indeed it is a VBA to VB.NET migration. I tried the suggestion you gave and it didn´t solve anything. I think it has to do with [font=Consolas][size=2][color=#0000ff]Dim[/color] midden [/size][size=2][color=#0000ff][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][/color][/size][/font][size=2][color=#0000ff][size=2][color=#0000ff][size=2][font=Consolas][color=#0000ff]Object [/color][/font][/size][/color][/size][/color][/size] But I dont know how that should be. I hope you know. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted February 9, 2012 Share Posted February 9, 2012 Perhaps it would be prudent for us to back up for a moment, given that this is a migration from VBA --> VB.NET to make sure some of the basics are covered first? This post may have some useful links to make sure that you have all of the necessary applications, and support files for coding with Visual Studio for your version of AutoCAD: In the short-term, have you installed the appropriate VBA Enabler for your version? Thinking more long-term, consider porting your code to VB.NET - You'll need Visual Studio 2010 (I use Express), the appropriate ObjectARX SDK(s), and I recommend you also use the current Wizard for properly debugging (when using Express). Once you've got all of that established, you'll want to (I think), be able to debug... presuming you're using Visual Studio 2010 Express, this post may come in handy: ... Consider adding a Script to your .NET project. :wink: Open a blank text file (Notepad?), and add the following: netload "[color=red]FileName[/color].dll" ... Save this file to your .NET project directory with a .SCR file extension. For this example, we'll use "autoNetload.scr" but you can use any name you like. Next, in your .NET project, go to > Project's Properties > Debug Tab > Start Options > Add this: /b "[color=red]VisualStudioProjectFilePath[/color]\[color=blue]autoNetload[/color].scr" Now Debug using Visual Studio... Tada! Your debug compiled .DLL was just NETLOADed automagically. Lemon Squeezy. ** Note- The reason that the script can load the DLL without providing a full file path is due to the fact that "Drawing1.dwg" is opened in the ..\bin\Debug\ folder. To confirm, simply try (getvar 'dwgprefix) at the command line. Separately, you may at some point want to code in Visual Studio (Express?) an application that checks for an existing instance of AutoCAD to either GetObject, or CreateObject, in which case this post may be of help: ** Note - This example uses an Excel Application Object, but can easily be adapted to Get/Create an AutoCAD Application Object. Sounds like you need to check for an active process, prior to creating a new instance. Here's a VB.NET snippet from one of my projects, for you to pull from: Imports Excel = Microsoft.Office.Interop.Excel Imports Microsoft.Office.Interop Imports System.Diagnostics Imports System.IO Module FOO Sub Excel_Foo() Dim xlApp As Excel.Application Try [color=seagreen]' Look for an existing process, if available getObject[/color] If Process.GetProcessesByName("Excel").Length > 0 Then xlApp = CType(GetObject(, "Excel.Application"), Excel.Application) [color=seagreen]' Else createObject[/color] Else xlApp = New Excel.Application End If [color=seagreen] ' <-- Do something[/color] Catch ex As Exception Finally xlApp = Nothing End Sub End Module HTH Quote Link to comment Share on other sites More sharing options...
BlackBox Posted February 9, 2012 Share Posted February 9, 2012 FWIW - AutoCAD .NET Developer's Guide > Control the AutoCAD Environment > Prompt for User Input > GetPoint Method Quote Link to comment Share on other sites More sharing options...
SEANT Posted February 10, 2012 Share Posted February 10, 2012 Try this: [font=Consolas][size=2][/size][/font] [font=Consolas][size=2][size=2][font=Consolas] <CommandMethod("BatM")> _ Public Sub BlockAtMid() Dim startpunt() As Double = New Double() {0.0, 0.0, 0.0} Dim eindpunt() As Double = New Double() {0.0, 0.0, 0.0} Dim midden() As Double = New Double() {0.0, 0.0, 0.0} Dim aanlegjaar As AcadBlockReference Dim streng As AcadLine Dim ThisDrawing As AcadDocument = Autodesk.AutoCAD.ApplicationServices.Application. _ DocumentManager.MdiActiveDocument.AcadDocument[/font][/size] [size=2][font=Consolas][/font][/size] [size=2][font=Consolas] ' Get the first point without entering a base point startpunt = ThisDrawing.Utility.GetPoint(, "What is the beginning of the line?: ") eindpunt = ThisDrawing.Utility.GetPoint(startpunt, "What is the end of the line?: ") midden(0) = (startpunt(0) + eindpunt(0)) / 2 midden(1) = startpunt(1) / 2 + eindpunt(1) / 2 midden(2) = startpunt(2) / 2 + eindpunt(2) / 2[/font][/size] [size=2][font=Consolas] streng = ThisDrawing.ModelSpace.AddLine(startpunt, eindpunt) aanlegjaar = ThisDrawing.ModelSpace.InsertBlock(midden, "C:\block 1.dwg", 1, 1, 1, 0)[/font][/size] [size=2][font=Consolas] End Sub [/font][/size][/size][/font] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.