Author Topic: C# - Newbie  (Read 2785 times)

0 Members and 1 Guest are viewing this topic.

Oak3s

  • Bull Frog
  • Posts: 321
C# - Newbie
« on: August 30, 2011, 06:13:34 PM »
I hope this is in the right location.

I have just started with C# and have been following various tutorials pretty well. Now I am tying to get this over to Autocad.

The code I have for the basic Hello World:
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace Lab_01
{
    public class Class1
    {
        [CommandMethod("HelloWorld")]
        public void HelloWorld()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("Hello World");
        }
    }
}

I believe I have the references I need. [acdbmgd and acmgd - both set to copy local false]

When I F5 I get the following:
"A project with an Output Type of Class Library cannot be started direcly. In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project."

Huh? :) I am 'trying" to use Microsoft Visual C# 2010 Express and Autocad 2011. Actually, Autocad Structural Detailing 2011...

If I NETLOAD in Autocad I get:
Code: [Select]
Cannot load assembly. Error details: System.BadImageFormatException: Could not
load file or assembly
'file:///C:\Users\edited\Lab-01.dll' or one of its dependencies. This assembly is built
by a runtime newer than the currently loaded runtime and cannot be loaded.
File name:
'file:///C:\Users\edited\Lab-01.dll'
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
   at loadmgd()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure
logging.
To turn this feature off, remove the registry value
[HKLM\Software\Microsoft\Fusion!EnableLog].

Any help would be appreciated.

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1365
  • class keyThumper<T>:ILazy<T>
Re: C# - Newbie
« Reply #1 on: August 30, 2011, 06:24:55 PM »

Rushing past , so briefly :
Google
AutoCAD "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded"
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

Oak3s

  • Bull Frog
  • Posts: 321
Re: C# - Newbie
« Reply #2 on: August 30, 2011, 06:58:33 PM »
Thank you kdub

The solution for anyone using this in the future:
Change the "Target framework" to the correct one :)

[Microsoft Visual C# 2010]
In 'Solution Explorer' Right click the project file > Properties > Application > Target framework

Mine was set to be .NET Framework 4.0. I changed it to .NET Framework 3.5

NETLOAD and got my 'Hello World".

Thanks again kdub.

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
Re: C# - Newbie
« Reply #3 on: August 30, 2011, 07:30:51 PM »
And for debugging using the Express versions there was this post by Kean:
http://through-the-interface.typepad.com/through_the_interface/2006/07/debugging_using.html
which has been updated by this one:
http://through-the-interface.typepad.com/through_the_interface/2011/08/debugging-autocad-net-projects-using-express-editions.html

And there are a few posts from 3-4 years ago here that Kerry posted on how to get it working (which happen to be what I used but I did not bookmark them :-( ).

BlackBox

  • King Gator
  • Posts: 3745
Re: C# - Newbie
« Reply #4 on: August 31, 2011, 12:12:27 AM »
Debugging in VS 2010 Express is simple.

(For either C#, or VB.NET)
Open the IDE, and crate a new project
Add AcDb.dll, and AcDbMgd.dll references (set both 'copy local' = False)
Save the project, and close IDE
In Windows Explorer, navigate to your saved project, and open the respective *.csproj, or *.vbproj file with Notepad
Now, add this code:

Snippet from <SomeFile>.vbproj:
Quote
  ...
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
   <StartAction>Program</StartAction>
   <StartProgram>C:\<FilePath>\acad.exe</StartProgram>
   ...
  </PropertyGroup>
  ...

Save the file
Reopen in IDE, and Debug at will (provided you've targeted the right NET framework)

** Note - you may need to modify a copy of acad.exe.config for breakpoints to be recognized.

Hope this helps!
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3745
Re: C# - Newbie
« Reply #5 on: August 31, 2011, 06:09:19 PM »
which has been updated by this one:
http://through-the-interface.typepad.com/through_the_interface/2011/08/debugging-autocad-net-projects-using-express-editions.html

^^ Gold :beer:

*Now if only I can get IT to install that thing!*

Edit: I got IT to install the wizard, but had to educate them on checking the .MSI's 'compatibility' setting, and remind them that they needed to install the ObjectARX SDK *first* to avoid the 'missing component' error.
*sigh*
« Last Edit: September 02, 2011, 12:39:11 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

Oak3s

  • Bull Frog
  • Posts: 321
Re: C# - Newbie
« Reply #6 on: September 01, 2011, 05:44:05 PM »
I wasnt sure what the subject title should have been when I started this thread. I usually like to name it something relevant...not a generic title like it is :)

If it should be changed...I hope it gets changed to something reflecting the content of the thread...maybe add a [solved]

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul