Jump to content

Help Needed Modifying Block Attributes on blocks already existing in DWG


Pilatus4me

Recommended Posts

The task seems relatively simple when I look at it but since I am so new to Autocad and more importantly how to interface VB.Net with AutoCad, the more I look at it the more I get confused.

 

I have a single file that has several blocks in it with various attributes. I can open it, save it as a different file name in vb.net but I cannot figure out how to modify the attributes in the blocks.

 

Here is what I want to do: Open the file, insert data into the attributes, save it as a different file name, modify the data again, save it as a different file name, etc etc

 

Thanks for any assistance even if you just point me in the right direction. My eyes are blurry from reading everything I can find on the subject.

Link to comment
Share on other sites

Welcome to CADTutor!

 

What you're after is to iterate each BlockReference in the target Document's Database, and subsequently iterate said BlockReference's AttributesCollection Object, modifying each Type-cast ObjectId as AttributeReference once opened in a Transaction via GetObject() Method.

 

This may be of use: http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-BA69D85A-2AED-43C2-B5B7-73022B5F28F8

 

Also, you might instead consider opening the drawing as a side Database instead of actually opening in the Editor. Much faster in terms of performance too.

 

 

 

Separately, if you're new to .NET development, then you might consider learning C# in lieu of VB as you're going to find significantly more sample code available to help you learn along the way.

 

Cheers

Link to comment
Share on other sites

If your not locked into .NET then there are just so many example code for VLISP or plain lisp here. Just ask and post a dwg so code can be customised to suit.

Link to comment
Share on other sites

Thanks for the information. For whatever reason, when I insert the code from the link you gave me, it compiles just fine but will not run the code. It just skips over it like there is something wrong. No errors are shown, it just doesn't run it.

 

It seems to be from whenever the code references:

 

 Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
   acCurDb = Application.DocumentManager.MdiActiveDocument.Database

 

I have all of the import statements included in the project and like I said no errors are generated when it compiles or during run time(that I can see).

 

I have narrowed it down to MdiActiveDocument anytime a statement is inserted that has this in it, the code will compile just fine, it just won't execute it.

 

And unfortunately I am locked into VB.net.

Link to comment
Share on other sites

Thanks for the information. For whatever reason, when I insert the code from the link you gave me, it compiles just fine but will not run the code. It just skips over it like there is something wrong. No errors are shown, it just doesn't run it.

 

It seems to be from whenever the code references:

 

 Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
   acCurDb = Application.DocumentManager.MdiActiveDocument.Database

 

I have all of the import statements included in the project and like I said no errors are generated when it compiles or during run time(that I can see).

 

I have narrowed it down to MdiActiveDocument anytime a statement is inserted that has this in it, the code will compile just fine, it just won't execute it.

 

And unfortunately I am locked into VB.net.

 

More information is needed.

 

What version of AutoCAD are you developing for (you list 2014 in your profile)?

 

What version of Visual Studio are you using?

 

What environment are you using?

 

Are you compiling/debugging for the appropriate environment?

 

Are you using .NET or COM API (you only posted two lines of .NET)?

 

If using COM API, are you late binding?

 

Did you download, and install the appropriate ObjectARX SDK & .NET Wizard?

 

What code are you using (post it)?

 

Did you load your assembly via Autoloader, Registry Loader, or manually via NETLOAD Command after being compiled?

 

Did you test for MdiActiveDocument != null (again, depending on version)?

Link to comment
Share on other sites

What version of AutoCAD are you developing for (you list 2014 in your profile)? Yes, 2014 64 bit full version not LT

 

What version of Visual Studio are you using? 2010 Express

 

What environment are you using? windows 7 64 bit

 

Are you compiling/debugging for the appropriate environment? yes

 

Are you using .NET or COM API (you only posted two lines of .NET)? .net

 

If using COM API, are you late binding?

 

Did you download, and install the appropriate ObjectARX SDK & .NET Wizard? Yes, I believe so, here is a screenshot from the references tab for the project.reference.jpg

 

What code are you using (post it)? Posted below. The rest of the code is currently commented out to try and isolate where the issue is coming from. A previous function opens the autocad document. I then wait for a time period to be sure everything is loaded and then move on to the code below. By doing it this way I can now get it to generate an error in which it states that it cannot find the dll.

 Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim doc As Document = Application.DocumentManager.MdiActiveDocument

 

here is the error I get now:

error.PNG

 

I have double checked and the files are actually where they are supposed to be.

Did you load your assembly via Autoloader, Registry Loader, or manually via NETLOAD Command after being compiled? Running it by stepping through line by line.

 

Did you test for MdiActiveDocument != null (again, depending on version)? Did not. Basically copied and pasted the code from autodesk's website.

Link to comment
Share on other sites

What version of AutoCAD are you developing for (you list 2014 in your profile)?

 

Yes, 2014 64 bit full version not LT

 

Thank you.

 

 

 

What version of Visual Studio are you using?

 

2010 Express

 

Visual Studio 2010 Express had standalone applications for each C++, C#, and VB each with a max .NET 4.0 Framework (if memory serves)... However, Autodesk instead recommends here that you use Visual Studio 2012 (Express ok) when developing for 2014.

 

 

 

What environment are you using?

 

windows 7 64 bit

 

Thank you.

 

 

 

Are you compiling/debugging for the appropriate environment?

 

yes

 

Are you sure? What are the Solution Configuration, and Solution Platforms dropdown menus set to?

 

 

 

Are you using .NET or COM API (you only posted two lines of .NET)?

 

.net

 

Then why does your screenshot below show the Interop (COM) assembly references?

 

 

 

Did you download, and install the appropriate ObjectARX SDK & .NET Wizard?

 

Yes, I believe so, here is a screenshot from the references tab for the project.

 

attachment.php?attachmentid=50577&d=1409154597

 

In the link above, download & install the appropriate version of the ObjectARX SDK (which you appear to have), and .NET Wizard.

 

 

 

What code are you using (post it)?

 

Posted below. The rest of the code is currently commented out to try and isolate where the issue is coming from. A previous function opens the autocad document. I then wait for a time period to be sure everything is loaded and then move on to the code below. By doing it this way I can now get it to generate an error in which it states that it cannot find the dll.

 

       Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim doc As Document = Application.DocumentManager.MdiActiveDocument

 

here is the error I get now:

 

attachment.php?attachmentid=50578&d=1409155234

 

I have double checked and the files are actually where they are supposed to be.

 

That is the same code you posted above, and equally ambiguous.

 

It is impossible to assist you with where your code is broken, if we cannot see the code. So unless just asking a general question and expecting nothing but at best entertaining opinions in response, kindly be less ambiguous.

 

That said....

 

What function opens 'the AutoCAD Document'?

 

Again, you've not specified here, or below at all how 'everything is loaded'... You're either loading the code from within AutoCAD process instance (a plug-in), or you're running a standalone application (.exe) from outside of AutoCAD; since you've still not posted your code, which is it?

 

 

 

Did you load your assembly via Autoloader, Registry Loader, or manually via NETLOAD Command after being compiled?

 

Running it by stepping through line by line.

 

That is not a valid response, as you cannot step line by line through something which has not been loaded (if attempting to debug a plug-in, but you've not posted your code, and so I know not if you're even coding yourself a plug-in or standalone executable?); re-read previous comment.

 

 

 

Did you test for MdiActiveDocument != null (again, depending on version)?

 

Did not. Basically copied and pasted the code from autodesk's website.

 

First rule of [AutoCAD .NET development]: [Never assume the sample code you've copied will compile].

Link to comment
Share on other sites

Then why does your screenshot below show the Interop (COM) assembly references?

 

Those are .Net. Take a look at the type column. They come with the objectarx package.

 

I do think I see where I am confused. I was trying to run this code as a simple windows form and not a class library from within autocad using the netload command.

 

Here is the code for opening autocad and the drawing that I was using in my original trials:

 

Dim AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument
       Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication

       AcadApp = New Autodesk.AutoCAD.Interop.AcadApplication

       AcadApp.Visible = True
       AcadDoc = AcadApp.Documents.Open("c:\projects\acad\mag.dwg")

 

Not sure if there is a way for me to do what I wanted from outside of Autocad. But if I can get it to work from the netload command that will work just fine for me as well. Especially since this is just something I want to help speed up a very cumbersome task.

Link to comment
Share on other sites

Those are .Net. Take a look at the type column. They come with the objectarx package.

 

I do think I see where I am confused. I was trying to run this code as a simple windows form and not a class library from within autocad using the netload command.

 

I recognize that you're not seeing it; that's why I'm trying to help point you in the right direction, which obviously requires sufficient information (else wastes both our time).

 

I also thank you for finally clarifying what you're trying to do (Windows Form Application).

 

What you've unknowingly overlooked, is that the ObjectARX SDK provides _both_ .NET and COM assemblies... The former are located in ..\inc\ directory, whereas the latter are located in either ..\inc-x64\ or ..\inc-win32\ directories as COM is environment dependent (.NET works in both 32-bit & 64-bit environments, hence 'AnyCPU', etc.).

 

You can successfully use COM in, and outside of AutoCAD, but identifying that leads to different recommendations as to a potential resolution to the behavior you're observing.

 

 

 

Here is the code for opening autocad and the drawing that I was using in my original trials:

 

       Dim AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument
       Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication

       AcadApp = New Autodesk.AutoCAD.Interop.AcadApplication

       AcadApp.Visible = True
       AcadDoc = AcadApp.Documents.Open("c:\projects\acad\mag.dwg")

 

Not sure if there is a way for me to do what I wanted from outside of Autocad. But if I can get it to work from the netload command that will work just fine for me as well. Especially since this is just something I want to help speed up a very cumbersome task.

 

That is COM code, and not .NET as only COM utilizes the Interop assembly references... This code also needs to be compiled to 64-bit assembly, since you're working in Win7x64 according to your description above.

 

Nearly all of my AutoCAD development is for plug-ins, and what little COM I used to implement, has since been ported over to .NET with the advent of the dynamic keyword, and P/Invoke.

 

Now, you can still do what you're after using your Forms app, but it's going to be somewhat slower, and certainly a bit more complex than simply doing so from within an instance of AutoCAD. Please see this post for more information on how to accomplish what you're after using .NET API. For more information on Autoloader, please see the link in my signature.

 

I'm sure you're still going to need to bounce code off of others before you get it doing exactly what you're after, and we're happy to help... Just need enough information & code to make it worth volunteering our time.

 

Cheers :beer:

Link to comment
Share on other sites

Just wanted to say thanks for all of the help. I know how much time it takes to provide this information. Going to go back and read through some of the documentation that I read before but with a mindset of building it as a dll instead of a windows app. It might appear clearer that way.

 

Thanks again.

Link to comment
Share on other sites

Just wanted to say thanks for all of the help. I know how much time it takes to provide this information. Going to go back and read through some of the documentation that I read before but with a mindset of building it as a dll instead of a windows app. It might appear clearer that way.

 

Thanks again.

 

You're welcome, Pilatus4me; I'm happy to help.

 

I look forward to seeing your progress in the coming posts. :thumbsup:

 

Cheers

Link to comment
Share on other sites

Update: Read through a bunch of material once again with the mindset of running it from within AutoCad. Now most of it makes sense.

 

Good news is that I now have it working like I want. Reading data from an excel spreadsheet and passing that information to the correct block and attribute within that block. Kind of fun to sit back and watch it go through 100's of autocad files, make the changes then save the file, close it and move on to the next one. I should turn my desk around at work so people walking by can be impressed at how fast and efficient I am at AutoCad. :lol::twisted:

 

Although not as simple as I think it should be, once I was pointed in the right direction it is something that could be done in a few hours for a relative newbie (to Autocad not VB).

 

Thanks again for pointing me in the right direction. You saved me a good bit of frustration.

Link to comment
Share on other sites

Update: Read through a bunch of material once again with the mindset of running it from within AutoCad. Now most of it makes sense.

 

Good news is that I now have it working like I want. Reading data from an excel spreadsheet and passing that information to the correct block and attribute within that block. Kind of fun to sit back and watch it go through 100's of autocad files, make the changes then save the file, close it and move on to the next one. I should turn my desk around at work so people walking by can be impressed at how fast and efficient I am at AutoCad. :lol::twisted:

 

Although not as simple as I think it should be, once I was pointed in the right direction it is something that could be done in a few hours for a relative newbie (to Autocad not VB).

 

Thanks again for pointing me in the right direction. You saved me a good bit of frustration.

 

Congratulations!

 

If you think that's fast, I think you'd be interested in comparing the speed of your current plug-in against a second that effectively does the same via side database as mentioned in my first post in this thread. :thumbsup:

 

Also, don't forget to post your code to share with the class, as you certainly aren't going to be the only one seeking to do something similar, and giving back is always appreciated. :beer:

 

Cheers

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...