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

Couple of Newbie Questions

20 REPLIES 20
Reply
Message 1 of 21
Anonymous
1171 Views, 20 Replies

Couple of Newbie Questions

Hi everyone

I just started programming in VB.NET and am trying to interface with AutoCAD... it's coming along decently so far but am having a few stumbling blocks... I was hoping that someone might have some insights into my issues...

-----

1) I've got a Class Module that I'm using Catch/Try in... this handling works perfectly (as expected) until I add 'Imports Autodesk.AutoCAD.Runtime'... once I do this, it's as if the error handling is just "not there"... by this, I mean that VS Express 2008 reacts to an error as if I didn't have the statements there... my errors either bump up to a calling subroutine OR VS reports an unhandled error...

2) I'm referencing Jerry Winters' book "VB.NET Programming for AutoCAD Customization"... I'm trying to adapt his code (all the examples are written for Class libraries and I am writing an application that interfaces externally with AutoCAD)... so I've entered this

Dim myDwg as Autodesk.AutoCAD.ApplicationServices.Document

myDwg = AutoDesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Trying to execute this code results in the following exception

"The type initializer for '' threw an exception"

Could anyone elaborate on what this means?

-----

Many thanks in advance!

-Justin
20 REPLIES 20
Message 2 of 21
Anonymous
in reply to: Anonymous

Just a small update... I seem to be having problems with *any* uses of DocumentManager...

Even this small example in a new project yields a cryptic "FileNotFoundException" *before* the code even processes the Dim statement...

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim file As Autodesk.AutoCAD.ApplicationServices.Document

file = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open("D:\Somefile.dwg", True)

End Sub

End Class
Message 3 of 21
Mikko
in reply to: Anonymous

Maybe a quick read can help.

http://discussion.autodesk.com/thread.jspa?messageID=5554613
Message 4 of 21
Anonymous
in reply to: Anonymous

You cannot use the Autocad .net libraries in an out of process application. If this must be a standalone app then
look into using ActiveX.

wrote in message news:6004459@discussion.autodesk.com...
Hi everyone

I just started programming in VB.NET and am trying to interface with AutoCAD... it's coming along decently so far but am having a
few stumbling blocks... I was hoping that someone might have some insights into my issues...

-----

1) I've got a Class Module that I'm using Catch/Try in... this handling works perfectly (as expected) until I add 'Imports
Autodesk.AutoCAD.Runtime'... once I do this, it's as if the error handling is just "not there"... by this, I mean that VS Express
2008 reacts to an error as if I didn't have the statements there... my errors either bump up to a calling subroutine OR VS reports
an unhandled error...

2) I'm referencing Jerry Winters' book "VB.NET Programming for AutoCAD Customization"... I'm trying to adapt his code (all the
examples are written for Class libraries and I am writing an application that interfaces externally with AutoCAD)... so I've entered
this

Dim myDwg as Autodesk.AutoCAD.ApplicationServices.Document

myDwg = AutoDesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

Trying to execute this code results in the following exception

"The type initializer for '' threw an exception"

Could anyone elaborate on what this means?

-----

Many thanks in advance!

-Justin
Message 5 of 21
Anonymous
in reply to: Anonymous

Hi Mikko

If I understand what you're saying correctly then the book I purchased by Jerry Winters (VB.NET Programming for AutoCAD Customization) is not not applicable for me...it deals only with the creation of Class Library .dll files that get loaded *into* AutoCAD, correct? And as such, its examples are not applicable...

---------

Here's what I am trying to do... maybe you can tell me the best approach and/or recommend a resource...

I would like to develop a Windows application (.EXE) that interacts with AutoCAD and MS Access... my database contains information that is to be populated into the drawing blocks/attributes...

Thanks!
Message 6 of 21
Anonymous
in reply to: Anonymous

So it seems that my options are to either develop the application as a standalone (using COM protocols) OR as a Netload .dll that makes use of .NET protocols...

After speaking with the author of the book, my understanding is that .NET is the future and COM (while it will remain supported) is legacy... in addition, COM is slower than .NET?

Any recommendations would be most appreciated...

Thanks!

-Justni
Message 7 of 21
Mikko
in reply to: Anonymous

What I recommend has no bearing, you have to produce the results whereas I don't.

Personally, I would scrape Access and go with SQL Server Express 2008 which will be released at the end of the month. You can use the 180 day Enterprise version till then. http://www.microsoft.com/express/sql/default.aspx or the 2005 version, but upgrade to 2008 for the new features when released.

Use one of the new 2008 Visual Studio editions or an Express version. The VB Express edition is found here. http://www.microsoft.com/express/vb/

I would use WPF because of the coolness factor and ease of use. One of the last hurdles, the datagrid, has just been released the other day. http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14963 most every thing else has been baked in.

I would use LINQ because its easy. http://msdn.microsoft.com/en-us/library/bb397926.aspx

Go with AutoCAD 2009 or at least 2008 and keep reading these forums and use the ACAD HELP : ADDITIONAL RESOURCES : DEVELOPER HELP which is almost a cut and paste along with some verbiage editing to make it work in VB.NET. Managed VB code is a different beast as you found out.

Don't look back.

Is COM slower? Got to put it in perspective. How long is it taking you to do what your doing now. How large are your drawings? For batching multiple directories with 1000's of drawings I've never had any problems with a stand-alone.exe program running unattended in my environment.

Lots of opinions here, best use your own judgement and run with it.
Message 8 of 21
Anonymous
in reply to: Anonymous

Hi Mikko

Thanks for all the information!

I'll have to look into the SQL Server Express at some point but for now, I think I'll be staying with Access... many of my users are not that familiar with Access and to change again would introduce difficulty...

Already using VB.NET Express 2008... regarding the usage of LINQ - I believe it says that I can use ADO.NET Datasets which is what I believe I am using to pull data from my Access database presently... if I understand my cursory read of LINQ correctly, I can write a LINQ query against a dataset of my entire table, correct?

What exactly is "managed code"?

Thanks again!

-Justin
Message 9 of 21
Anonymous
in reply to: Anonymous

Hi Mikko

The docs on LINQ discuss it being able to be used on any object that supports IsEnumerable (or something like that)... would I be able to uses to query for a specific attribute in a block?
Message 10 of 21
Anonymous
in reply to: Anonymous

on the sql suggestion, what if I need databases that are not on a server?
mdb works, but has anyone tried using an xml file as the database?
You could load it into a dataset, and have the ds write back as needed.
I guess that would not allow concurrent use though. I'm more interested in speed though.
Are there any other good "standalone" database formats that work with ADO.net?

Mikko <>
|>What I recommend has no bearing, you have to produce the results whereas I don't.
|>
|>Personally, I would scrape Access and go with SQL Server Express 2008 which will be released at the end of the month. You can use the 180 day Enterprise version till then. http://www.microsoft.com/express/sql/default.aspx or the 2005 version, but upgrade to 2008 for the new features when released.
|>
|>Use one of the new 2008 Visual Studio editions or an Express version. The VB Express edition is found here. http://www.microsoft.com/express/vb/
|>
|>I would use WPF because of the coolness factor and ease of use. One of the last hurdles, the datagrid, has just been released the other day. http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14963 most every thing else has been baked in.
|>
|>I would use LINQ because its easy. http://msdn.microsoft.com/en-us/library/bb397926.aspx
|>
|>Go with AutoCAD 2009 or at least 2008 and keep reading these forums and use the ACAD HELP : ADDITIONAL RESOURCES : DEVELOPER HELP which is almost a cut and paste along with some verbiage editing to make it work in VB.NET. Managed VB code is a different beast as you found out.
|>
|>Don't look back.
|>
|>Is COM slower? Got to put it in perspective. How long is it taking you to do what your doing now. How large are your drawings? For batching multiple directories with 1000's of drawings I've never had any problems with a stand-alone.exe program running unattended in my environment.
|>
|>Lots of opinions here, best use your own judgement and run with it.
James Maeding
Civil Engineer and Programmer
jmaeding - at - hunsaker - dotcom

Does Autodesk issue service packs on
the release you are using? not likely.
Message 11 of 21
Anonymous
in reply to: Anonymous

Hi Mikko

How does one handle variable field types in LINQ? By this, I mean that I am retrieving data from a field in my database but the field type is not always the same... for instance, I have the following code

'Update Block Attribute Value from Database Information Table
AutoCAD_Block_Attribute.TextString = ADG_Component_Record.Field(Of String)(strAutoCAD_Block_Attribute_Database)

When my field is NOT a string, it generates an exception... I tried making it 'Of VariantType' but that doesn't seem to work...

Can anyone elaborate on how to handle this?

Many thanks!

-Justin
Message 12 of 21
Anonymous
in reply to: Anonymous

My suggestion would be to spend some time on your own, learning the basics, before trying to write real applications.

The 'basics' in this case, are things like Object.ToString(), which is something every .NET programmer should know about and understand, before they actually try to write real working code.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6007138@discussion.autodesk.com...
Hi Mikko

How does one handle variable field types in LINQ? By this, I mean that I am retrieving data from a field in my database but the field type is not always the same... for instance, I have the following code

'Update Block Attribute Value from Database Information Table
AutoCAD_Block_Attribute.TextString = ADG_Component_Record.Field(Of String)(strAutoCAD_Block_Attribute_Database)

When my field is NOT a string, it generates an exception... I tried making it 'Of VariantType' but that doesn't seem to work...

Can anyone elaborate on how to handle this?

Many thanks!

-Justin
Message 13 of 21
Anonymous
in reply to: Anonymous

Hi Tony

I thought I understood the concept of .ToString but based on your statement, I'm not so sure that I do anymore... what I think I'm struggling with is how to handle the LINQ syntax; am I mistaken?

I believe my issue is that .Field(of T) requirement... I'm trying to tell it to access an Int32 field as a string and it's telling me that's invalid...
Message 14 of 21
Anonymous
in reply to: Anonymous

Sorry, not sure what you mean by the 'LINQ syntax'.

You're assigning a value to a property that is a string, so you must give it a string type. It has nothing to do with LINQ.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6007333@discussion.autodesk.com...
Hi Tony

I thought I understood the concept of .ToString but based on your statement, I'm not so sure that I do anymore... what I think I'm struggling with is how to handle the LINQ syntax; am I mistaken?

I believe my issue is that .Field(of T) requirement... I'm trying to tell it to access an Int32 field as a string and it's telling me that's invalid...
Message 15 of 21
Anonymous
in reply to: Anonymous

Hi Tony

I understand that I need to use the .ToString to store an Int32 into a String... but what I am asking is how do I handle the

.Field(Of String) syntax... it seems to me that it's generating an exception because I'm trying to access a Int32 field in my query with a String type... I added the .ToString to the end of the command without any change in result...

Am I just missing something here?
Message 16 of 21
Anonymous
in reply to: Anonymous

The problem you're having has nothing specifically to do with LINQ.

It's a result of your not having a basic level of familarity with .NET, VB.NET, and the use of Generics. Or put more simply, you're trying to run before you've first learned to walk. 😉

The "(Of String)" in 'Field(Of String)' is a generic argument.

As far as what generics are and how they're used, I would suggest you put your current goal of trying to use LINQ to do AutoCAD development on the back burner for the time being, and first make a personal effort to actually learn the prerequisites, like the programming langauge; CLR language features like Generics; and the .NET framework.

Try googling "Introduction to Generics In .NET". The first few hits you get should be useful.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6008581@discussion.autodesk.com...
Hi Tony

I understand that I need to use the .ToString to store an Int32 into a String... but what I am asking is how do I handle the

.Field(Of String) syntax... it seems to me that it's generating an exception because I'm trying to access a Int32 field in my query with a String type... I added the .ToString to the end of the command without any change in result...

Am I just missing something here?
Message 17 of 21
Anonymous
in reply to: Anonymous

Hi Tony

Thank you for the suggestions... I've taken an initial look at the search you recommended and that seems most interesting... will naturally take time to digest how best to make use of these lessons...

One question, if I may... most of the results I found seemed to deal with the developer writing their own classes and making use of the generics so as to avoid duplication for each desired data type... but the .Field(Of String) is not my class, right? It's part of a class already developed by the VB.NET framework, right?

I believe I understand how the .Field function was developed using generics to allow the user flexibility in data types but what I still don't understand is how I should handle writing my LINQ query when I don't know the data type of the field that I am querying against... if I know my field is an Int32, String, etc - I can just use Of 'appropriate type' BUT if I do not know what it is...

So far, I've been able to deal with this by using 'Of Object' but I suspect that this is probably not the best course of action...

Dim DatabaseInformation_Table_Query = From Component_Record In DatabaseInformation_Table.AsEnumerable() Select Component_Record _
Where (Component_Record.Field(Of Object)(strReferenceAttribute_Field) = strAutoCAD_Block_ReferenceAttributeValue)

The other thing that I am concerned about is that the articles mentioned you don't want to really use Object (as opposed to the generic in your classes) because of the type casting and its computational intensiveness... here I believe I am type-casting the variable 'strAutoCAD_Block_ReferenceAttributeValue' (which is a string as that's the type of an attribute .TextString)...

Thanks again!

-Justin
Message 18 of 21
Anonymous
in reply to: Anonymous

The examples should show generics usage as well as using them in user-defined classes. You may have to look a little further since even Microsoft's own docs for VB.NET do not seem to discuss them at all (the C# docs do however).

As far as handling different types, if you don't know the type at runtime, you have to deal with the value as a System.Object because that's the only way to get a string.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6008801@discussion.autodesk.com...
Hi Tony

Thank you for the suggestions... I've taken an initial look at the search you recommended and that seems most interesting... will naturally take time to digest how best to make use of these lessons...

One question, if I may... most of the results I found seemed to deal with the developer writing their own classes and making use of the generics so as to avoid duplication for each desired data type... but the .Field(Of String) is not my class, right? It's part of a class already developed by the VB.NET framework, right?

I believe I understand how the .Field function was developed using generics to allow the user flexibility in data types but what I still don't understand is how I should handle writing my LINQ query when I don't know the data type of the field that I am querying against... if I know my field is an Int32, String, etc - I can just use Of 'appropriate type' BUT if I do not know what it is...

So far, I've been able to deal with this by using 'Of Object' but I suspect that this is probably not the best course of action...

Dim DatabaseInformation_Table_Query = From Component_Record In DatabaseInformation_Table.AsEnumerable() Select Component_Record _
Where (Component_Record.Field(Of Object)(strReferenceAttribute_Field) = strAutoCAD_Block_ReferenceAttributeValue)

The other thing that I am concerned about is that the articles mentioned you don't want to really use Object (as opposed to the generic in your classes) because of the type casting and its computational intensiveness... here I believe I am type-casting the variable 'strAutoCAD_Block_ReferenceAttributeValue' (which is a string as that's the type of an attribute .TextString)...

Thanks again!

-Justin
Message 19 of 21
Anonymous
in reply to: Anonymous

Ok, thanks Tony for the help...
Message 20 of 21
foruma000256
in reply to: Anonymous

while the field(of T) is a bit over my head, the concept here is not. Try this:

'Create a string variable:
Dim str as String = string.empty

'get value from database and convert it to a string
str = cstr(ADG_Component_Record.Field(Of whatever the object type is in the database record))

'apply a STRING value to a STRING property
AutoCAD_Block_Attribute.TextString = str

That's why Tony says back to basics. Sometimes we overlook the simple things getting into advanced programming, and need help to look back.

jvj

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