←Select platform

OnInsertFile Method

Summary
Called for each DICOM file enumerated by the InsertFile method.
Syntax
C#
Objective-C
C++/CLI
- (LTDicomDirInsertFileCommand)onInsertFile:(NSString *)fileName dataSet:(LTDicomDataSet *)dataSet status:(LTDicomDirInsertFileStatus)status errorCode:(LTDicomErrorCode)code; 

Parameters

fileName
The name of the file being added to the DICOM Directory.

dataSet
A DicomDataSet object that specifies the Data Set contained by the DICOM file being processed. If the Data Set wasn't loaded successfully, this parameter is set to a null reference (Nothing in VB).

status
The status code.

error
The error code. This parameter is always set to DicomExceptionCode.Success unless the parameter status is set to DicomDirInsertFileStatus.Failure.

Return Value

A value indicating how the enumeration of DICOM files should continue.

Remarks

If the parameter passed to InsertFile is a null reference (Nothing in VB), then all the files in the destination folder, and optionally all the files in the subfolders of the destination folder, will be enumerated. For each file, the callback method OnInsertFile is called once or twice:

  • OnInsertFile is called just after trying to load the Data Set in the file and before actually adding the file to the DICOM Directory. If the Data Set was loaded successfully, the parameter [dataSet](" id="datasetparameterlink" class="popuplink.html) specifies the loaded Data Set and the parameter status is set to DicomDirInsertFileStatus.PreAdd. To skip the addition of the file, OnInsertFile should return DicomDirInsertFileCommand.Skip. If the Data Set wasn't loaded successfully, the parameter [dataSet](" id="datasetparameterlink" class="popuplink.html) is set to a null reference, the parameter status is set to DicomDirInsertFileStatus.Failure, and the parameter [error](" id="errorparameterlink" class="popuplink.html) is set to an error code.
  • OnInsertFile is called the second time just after trying to add the loaded Data Set to the DICOM Directory. The parameter [dataSet](" id="datasetparameterlink" class="popuplink.html) specifies the loaded Data Set. If the Data Set was added successfully, the parameter status is set to DicomDirInsertFileStatus.Success. Otherwise, it is set to DicomDirInsertFileStatus.Failure and the parameter [error](" id="errorparameterlink" class="popuplink.html) is set to an error code. This call to OnInsertFile is not made if a loading failure was reported in the first call, or the file addition was skipped in the first call.

If this method returned DicomDirInsertFileCommand.Stop, the method InsertFile will stop adding new files. Therefore, to keep adding the DICOM files, this method should return DicomDirInsertFileCommand.Continue, or DicomDirInsertFileCommand.Skip if only the current file is to be skipped (which can be done during the first call).

The method InsertFile will stop adding new files and will throw an exception if an error occurred.

The implementation of the DicomDir class for this method simply returns DicomDirInsertFileCommand.Continue. In order to make use of this callback method, you should create a new class derived from the DicomDir class and override the method providing the desired implementation.

Example
C#
using Leadtools; 
using Leadtools.Dicom; 
 
 
public class MyDicomDir : DicomDir 
{ 
   public MyDicomDir(string destinationFolder, string path) : base(destinationFolder, path) { } 
   public override DicomDirInsertFileCommand OnInsertFile(string fileName, DicomDataSet ds, DicomDirInsertFileStatus status, DicomExceptionCode code) 
   { 
      if (status == DicomDirInsertFileStatus.PreAdd) 
      { 
         string msg = string.Format("About to add the file '{0}'{1}{1}Proceed?", fileName, Environment.NewLine); 
         Console.WriteLine($"{msg}"); 
         return DicomDirInsertFileCommand.Continue; 
 
      } 
      else if (status == DicomDirInsertFileStatus.Success) 
      { 
         string msg = string.Format("The file '{0}' has been added successfully.", fileName); 
         Console.WriteLine(msg); 
      } 
      return DicomDirInsertFileCommand.Continue; 
   } 
}; 
 
public void CreateDicomDirectory() 
{ 
   //Make sure to initialize the DICOM engine, this needs to be done only once  
   //In the whole application 
   DicomEngine.Startup(); 
   using (MyDicomDir dicomDir = new MyDicomDir(null, null)) 
   { 
      bool autoSearch = true; 
      // Set the destination folder 
      dicomDir.Reset(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1")); 
      // Set the File-set Identifier 
      dicomDir.SetFileSetId("SOME ID"); 
      // Set some options 
      DicomDirOptions options = dicomDir.Options; 
      dicomDir.ResetOptions(); 
      options.IncludeSubfolders = true; 
      options.InsertIconImageSequence = false; 
      options.RejectInvalidFileId = true; 
      dicomDir.Options = options; 
 
      // Set the File-set descriptor file 
      string path = Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\README"); 
      dicomDir.SetDescriptorFile(path, null); 
 
      if (autoSearch) 
      { 
         try 
         { 
            dicomDir.InsertFile(null); 
         } 
         catch (Exception ex) 
         { 
            Console.WriteLine($"{ex.GetType().ToString()} {ex.Message}"); 
         } 
      } 
      else 
      { 
         // Load a Data Set and add it to the Directory 
         using (DicomDataSet ds = new DicomDataSet()) 
         { 
            ds.Load(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\Image1"), DicomDataSetLoadFlags.None); 
            dicomDir.InsertDataSet(ds, Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE1")); 
         } 
 
         // Add some DICOM files to the Directory 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE2")); 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE3")); 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE4")); 
      } 
 
      // Test the DataSet 
      DicomElement first = dicomDir.DataSet.FindFirstElement(null, DicomTag.PatientID, false); 
      Console.WriteLine(first.Length.ToString()); 
 
      // Save the DICOMDIR File 
      dicomDir.Save(); 
      //We can now call something like this to call the DICOMDIR back 
      //dicomDir.Load(LeadtoolsExamples.Common.ImagesPath.Path + @"TEMP\DCMTEST\PATIENT1\DICOMDIR",DicomDataSetLoadFlags.LoadAndClose); 
   } 
   DicomEngine.Shutdown(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.1.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Dicom Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.