2.4 Hello World Example |
The "Hello world" example in the laser business is marking a simple rectangle. How to do this with the RAYGUIDE API is shown in this section.
As mentioned before we deliver our SDK DLLs packed into NuGet packages, and Visual Studio must be able to find them. See section 2.2 Software how this is achieved.
In Visual Studio, create a new project and choose the "Console App" template.
Give it an arbitrary name, e. g. "HelloWorld".
Select ".NET 8.0 (Long Term Support)" as framework.
Change the Target OS to Windows.
In Visual Studio's Solution Explorer right-click on the project and open the Properties. Navigate to section Application/General and change the Target OS from None to Windows.
Add the needed NuGet package.
For this example only the package "RAYLASE.Marker" must be added to the project. Add it by right-clicking the project in Visual Studio's Solution Explorer, choose menu item Manage NuGet Packages..., select the Package source "RAYGUIDE SDK" which we added in section 2.2 Software and install the package "RAYLASE.Marker".
Type the source code provided in the next but one section into the Program.cs file.
Compile and run the application.
For your convenience the "Hello World" project amongst others can be found in folder C:\Program Files\RAYLASE\RAYGUIDE\SDK\SampleCode\Tutorials\.
Hint: If you want to create a .NET 4.8 application, chose the template "Console APP (.NET Framework)" and, when adding the NuGet package "RAYLASE.Marker", select the Package Manager Format "PackageReference in project file" when asked.
After asking for the SP-ICE-3 card's IP address the sample performs basically the following steps:
Create an instance of the API class.
Create the needed device objects, namely
a SP-ICE-3 device object,
a scan head object and
a laser device object.
The scan head and laser device objects need to be assigned to the SP-ICE-3 device object, and the latter must be provided with the IP address of the real SP-ICE-3 card.
Create a job containing the rectangle.
Execute the job.
Free the API resources before exiting the application.
//----------------------------------------------------------------------------- // This example demonstrates how to mark a simple rectangle and what // steps are needed to achieve this with the RAYGUIDE API. // The application will ask for the IP address of the SP-ICE-3 card. This // parameter could be given as command line parameter as well, e. g.: // HelloWorld.exe 169.254.0.98 //----------------------------------------------------------------------------- using GlmSharp; using RAYLASE.Marker; using RAYLASE.Marker.Device.ScanController; using RAYLASE.Marker.InternalPlugin.JobExecutors; using RAYLASE.Marker.Job; using RAYLASE.Marker.Job.Executor; using RAYLASE.Marker.Profile.Model; using RAYLASE.MarkerPlugin.Devices.Laser; using RAYLASE.MarkerPlugin.Devices.ScanController.SPICE3; using RAYLASE.MarkerPlugin.Devices.ScanHead; using RAYLASE.MarkerPlugin.JobElements; using System; namespace HelloWorld { class Program { static void Main( string[] args ) { Console.WriteLine( "\n\nHello World sample\n------------------\n\n" ); string ipAddress; // read the command line or enter the needed IP address: if ( args.Length == 1 ) { ipAddress = args[0]; } else { Console.WriteLine( "Enter SP-ICE-3 IP address" ); ipAddress = Console.ReadLine(); } // 1. Create the API: using ( MarkerAPI markerAPI = new() ) { // 2. Create and assign the devices: // 2.a) Create the SP-ICE-3 device: BaseScanController scanController = (BaseScanController)markerAPI.DeviceManager.AddDevice( "my SP-ICE-3 card", typeof( SPICE3Device ) ); scanController.IPAddress = ipAddress; scanController.EnableLogging = true; // 2.b) Create and assign the scan head: GenericScanHead scanHead = (GenericScanHead)markerAPI.DeviceManager.AddDevice( "my scan head", typeof( GenericScanHead ) ); scanController.AssignScanHead( scanHead ); // 2.c) Create and configure the laser device as needed by your hardware: scanController.LaserController = (CO2LaserDevice)markerAPI.DeviceManager.AddDevice( "my laser", typeof( CO2LaserDevice ) ); scanController.LaserController.LaserProfile.HotPowerTarget = PowerTarget.LmWidth; scanController.LaserController.LaserProfile.FpsPolarity = Polarity.ActiveHigh; scanController.LaserController.LaserProfile.GatePolarity = Polarity.ActiveHigh; scanController.LaserController.LaserProfile.LMPolarity = Polarity.ActiveHigh; // 2.d) Connect with the hardware: try { scanController.Initialize(); } catch ( ApplicationException ex ) { throw new ApplicationException( $"Initializing SP-ICE-3 card with IP address {scanController.IPAddress} failed.", ex ); } // 3. Create a sample job: // 3.a) Create a new job and add your card to it: IJobManager jobManager = markerAPI.JobManager; JobDefinition jobDefinition = jobManager.CreateNewJob( "Hello World" ); jobDefinition.ScanControllers.Add( scanController ); // 3.b) Create a rectangle of size 20mm x 10mm: MarkableRectangle rectangle = new() { Size = new dvec2( 50000, 30000 ) }; rectangle.MoveTo( new dvec3( 0, 0, 0 ) ); VectorGraphicMarkerProfile profile = (VectorGraphicMarkerProfile)rectangle.MarkableConfiguration.MarkerProfile; profile.MarkingMode = MarkingMode.OutlineFilling; // 3.c) Set the rectangle's pen parameters: rectangle.MarkerProfile.PenSet[1].LaserPower = 0.1; // be careful with the power this first time ... rectangle.MarkerProfile.PenSet[1].MarkSpeed = 1; rectangle.MarkerProfile.PenSet[1].JumpSpeed = 5; // 3.d) Add the rectangle to the job: jobDefinition.AddJobElement( rectangle ); // 4. Execute the job: // 4.a) Prepare the executor: BaseJobExecutorProfile jobExecutorProfile = jobManager.GenerateJobExecutorProfile( typeof( OnHostJobExecutor ) ); // 4.b) Arm the laser: scanController.LaserController.ArmLaser( true ); // 4.c) Execute the job: try { Console.WriteLine( $"Execute the job ..." ); jobManager.RunSync( jobDefinition, jobExecutorProfile ); } catch ( ApplicationException ex ) { throw new ApplicationException( $"Unable to execute job '{jobDefinition.Label}'.", ex ); } } // 5. Dispose the API: // markerAPI.Dispose(); // is done implicitely by means of the using statement above if ( args.Length == 0 ) // wait for keypress only if the user has been prompted for the IP address { Console.WriteLine( "Press any key to continue" ); Console.ReadKey(); } } } }