15 Replies Latest reply on Nov 17, 2017 11:19 AM by Josh Brady

    Can't find the instance of a sketch block definition on a drawing sheet.

    Greg Ashmore

      I have a SketchBlockDefinition in a drawing.

      The drawing has just one sheet.

      There is an instance of the sketch block definition in the sheet format.

      I activated the sheet and retrieved the first view, which I believe is the sheet view.

      I got the sketch for the view and called GetSketchBlockInstances.

      The return was Nothing.

       

      I expected to get a reference to the block instance that is in the sheet format.

       

      In this drawing, which has only one instance of the sketch block definition, I could get the block definition from the document sketch manager, and retrieve the instance from the block definition.

       

      But in the general case, there may be multiple sheets, each sheet having its own instance of the block definition in the sheet format.

      I need to discover the location of each instance in its sheet so that I can insert an entity at an offset from that location.

       

      I can get the sketch from the instance, but I don't see anyway of determining which sheet the sketch is associated with.

      So I have no way of associating the instance with a sheet.

       

      How can I get the instance of the sketch block definition in a sheet format?

       

      Thanks.

       

      Here is the code:

         Private Shared Sub DatamatrixInstanceLocationsGet()

            Dim nInstanceSeen As Boolean

            Dim iSheetCur As Integer

            Dim sSheetName As String

            Dim oMathPoint As MathPoint

            Dim oSketch As Sketch

            Dim oaSketchBlockInstances() As Object

            Dim oSketchBlockInstance As SketchBlockInstance

            Dim oView As SolidWorks.Interop.sldworks.View

       

            iSheetCur = 0

            While iSheetCur < iSheetCount

               sSheetName = oaSheetNames(iSheetCur)

               oDrawingModel.ActivateSheet(sSheetName)

               oView = oDrawingModel.GetFirstView

               oSketch = oView.GetSketch

               nInstanceSeen = False

               oaSketchBlockInstances = oSketch.GetSketchBlockInstances <<<<<<<<<<< returns Nothing

               If oaSketchBlockInstances IsNot Nothing Then

                  For Each oSketchBlockInstance In oaSketchBlockInstances

                     If oSketchBlockInstance.Definition.FileName = sDatamatrixBlockDefinitionFileName Then

                        oMathPoint = oSketchBlockInstance.InstancePosition

                        faDatamatrixBlockOriginX(iSheetCur) = oMathPoint.ArrayData(0)

                        faDatamatrixBlockOriginY(iSheetCur) = oMathPoint.ArrayData(1)

                        nInstanceSeen = True

                     End If

                  Next

               End If

               If Not nInstanceSeen Then

                  Throw New Exception("Datamatrix sketch block not found on sheet " & (iSheetCur + 1).ToString)

               End If

       

               iSheetCur += 1

            End While

       

       

            oMathPoint = Nothing

            oSketch = Nothing

            oaSketchBlockInstances = Nothing

            oSketchBlockInstance = Nothing

            oView = Nothing

         End Sub

        • Re: Can't find the instance of a sketch block definition on a drawing sheet.
          Alex Burnett

          It looks like you're on the right track, however I don't know if you can get sketch blocks from a sketch. I believe you have to have the SketchBlockDefinition object and then call its .GetInstances() function in order to get its instances in a particular drawing. I do believe the array it returns encompasses all sheets so you won't need to iterate through sheets and views.

           

          Here's a bit of the c# code I'm using to find mine. I know it doesn't directly translate syntax-wise but the process of getting what you need is the same.

           

          var sketchManager = model.SketchManager;
          
          ISketchBlockDefinition[] blockDefs = sketchManager.GetSketchBlockDefinitions();
          ISketchBlockDefinition foundBlock = null;
          if (blockDefs != null && blockDefs.Length > 0)
          {
               foreach (ISketchBlockDefinition blockDef in blockDefs)
               {
                    string[] fileNameSplit = blockDef.FileName.ToUpper().Split(new string[] { @"\", ".SLDBLK" }, StringSplitOptions.RemoveEmptyEntries);
                    string name = fileNameSplit[fileNameSplit.Length - 1];
                    if (string.Equals(name, "Name_I_Am_Looking_For"))
                    {
                         foundBlock = blockDef;
                         if (!foundBlock.isNull)
                         {
                              ISketchBlockInstance[] instances = foundBlock.GetInstances();
                              ISketchBlockInstance thisinstance = null;
                              if (instances != null && instances.Count() > 0)
                              {
                                   foreach (ISketchBlockInstance inst in instances)
                                   {
                                        thisinstance = inst;
                                        thisinstance.Visible = swAnnotationVisibilityState_e.swAnnotationVisible; // or do whatever you need to with the block instance.
                                   }
                              }
                         }
                    }
               }
          }
          

           

          Edit: changed variable types back to SW names

            • Re: Can't find the instance of a sketch block definition on a drawing sheet.
              John Alexander

              Just to reinforce this point; according to the documentation, the Sketch.GetSketchBlockInstances method will work for Parts or Assemblies but is not recommended for Drawings.

               

              2017 SOLIDWORKS API Help - Block Definitions and Block Instances

               

                • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                  Greg Ashmore

                  John,

                   

                  I see. I read that text in the manual, but missed the inference that instances are not accessible from the containing sketch in a drawing.

                   

                  I'm working on a method that puts a circle of a peculiar radius at the origin of the block definition. This circle will be exploded into the sheet sketch, and discovered while editing the sheet format. The center of the circle in the sheet sketch will be the reference for the inserted picture. Then the circle will be deleted.

                   

                  Kind of ugly, but workable. The circle is too small to be of practical use--and we never put circles in the sheet format anyway--so very low chance of finding the wrong circle.

                   

                  Greg

                • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                  Greg Ashmore

                  Alex,

                   

                  Thanks for your quick and clear reply.

                   

                  The difficulty with getting the sketch block instances from the sketch block definition is that I can't figure out how to determine which sheet a particular instance is in.

                   

                  I need to know the coordinates of the instance in its sheet. This because I am going to insert a picture in the sheet at some offset from the location of the block instance (which may differ from sheet to sheet) . That insertion has to be done while editing the sheet format; thus the insertion will be in the sheet sketch.

                   

                  The coordinates of the instance are readily available. That doesn't help much, because I don't have any way to determine which sketch the instance is in.

                   

                  Greg

                    • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                      John Alexander

                      A couple years ago, I  wrote a macro for inserting sketch blocks that would act as balloons on an assembly drawing. This snippet is from that macro:

                       

                      I used the word "balloon" to refer to the sketch blocks that were being used as balloons.

                       

                      Public Function findBalloonInstancesInView(swView As SldWorks.View, swSketchBlockDefinitionQuery() As SldWorks.SketchBlockDefinition, allBalloonsOnView() As SldWorks.SketchBlockInstance)

                          Dim swSketch                    As SldWorks.Sketch

                          Dim swSketchBlockDefinition     As SldWorks.SketchBlockDefinition

                          Dim swSketchBlockInstance       As SldWorks.SketchBlockInstance

                          Dim vView                       As Variant

                          Dim vSketchBlockDefinition      As Variant

                          Dim vSketchBlockInstance        As Variant

                          Dim count                       As Integer

                         

                          ReDim allBalloonsOnView(0)

                          count = 0

                         

                          If IsArrayAllocated(swSketchBlockDefinitionQuery) Then

                              For Each vSketchBlockDefinition In swSketchBlockDefinitionQuery

                                  Set swSketchBlockDefinition = vSketchBlockDefinition

                                  If IsArrayAllocated(swSketchBlockDefinition.GetInstances) Then

                                      For Each vSketchBlockInstance In swSketchBlockDefinition.GetInstances

                                          Set swSketchBlockInstance = vSketchBlockInstance

                                          Set swSketch = swSketchBlockInstance.GetSketch

                                          If swView.GetSketch Is swSketch Then

                                              ReDim Preserve allBalloonsOnView(count)

                                              Set allBalloonsOnView(count) = swSketchBlockInstance

                                              count = count + 1

                                          End If

                                      Next vSketchBlockInstance

                                  End If

                              Next vSketchBlockDefinition

                          End If

                      End Function

                       

                      Basically, I iterate over all of the SketchBlockInstances for a set of SketchBlockDefinitions and compare their SketchBlockInstance.Sketch to the View.Sketch.

                       

                      Since you are just looking for a single sketchblockinstance, this process can be a lot simpler.

                        • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                          Greg Ashmore

                          John,

                           

                          The idea of comparing SketchBlockInstance.Sketch to View.Sketch is good.

                          I'll give it a try after I resolve this new glitch relative to View.Sketch.

                          When I explode the block instances, the entities do not appear in View.Sketch for the first view in the sheet.

                          Likewise, when I insert the picture, it does not appear in View.Sketch.

                          The entities from the block instance, and the picture, are found in SketchManager.ActiveSketch.

                          And, SketchManager.ActiveSketch is somehow related to the sheet, because the exploded entities and inserted pictures appear in the appropriate sheet formats.

                           

                          Code, with explanatory comments:

                             Private Shared Sub DatamatrixImagesInsert()

                                Dim iSheetCur As Integer

                                Dim sSheetName As String

                                Dim fTestRadius As Double

                                Dim fTestDelta As Double

                                Dim fOriginX As Double

                                Dim fOriginY As Double

                                Dim oCircle As SketchArc

                                Dim oSketch As Sketch

                                Dim oSketchPicture As SketchPicture

                                Dim oSketchPictureNew As SketchPicture

                                Dim oaSketchPicturesAfter() As Object

                                Dim oaSketchPicturesBefore() As Object

                                Dim cSketchPicturesBefore As Collection

                                Dim oSketchPoint As SketchPoint

                                Dim oaSketchBlockInstances() As Object

                                Dim oSketchBlockInstance As SketchBlockInstance

                                Dim oSketchSegment As SketchSegment

                                Dim oaSketchSegments() As Object

                                Dim oView As SolidWorks.Interop.sldworks.View

                           

                                iSheetCur = 0

                                While iSheetCur < iSheetCount

                                   sSheetName = oaSheetNames(iSheetCur)

                                   oDrawingModel.ActivateSheet(sSheetName)

                                   oView = oDrawingModel.GetFirstView

                                   oDrawingDoc.EditTemplate()

                                   oSketch = oSketchManager.ActiveSketch  ' oView.GetSketch

                           

                          ^^^^^ I originally set the sketch to View.Sketch.

                          But neither the circle (from the exploded block instance) nor the inserted picture were found in the following code, for either of the two sheets.

                          After using SketchManager.ActiveSketch, I found the circle and the picture in sheet one, but not sheet two.

                          So I am getting what I want in sheet 1, not sheet two.

                          Not sure what to poke around at to discover what is going on.

                           

                                   ' Find the circle that was in the datamatrix sketch block, now exploded into this sketch.

                                   fOriginX = 0

                                   fOriginY = 0

                                   oaSketchSegments = oSketch.GetSketchSegments

                                   If oaSketchSegments IsNot Nothing Then

                                      For Each oSketchSegment In oaSketchSegments

                                         If oSketchSegment.GetType = swSketchSegments_e.swSketchARC Then

                                            oCircle = DirectCast(oSketchSegment, SketchArc)

                                            fTestRadius = oCircle.GetRadius

                                            fTestDelta = fCircleSize - fTestRadius

                                            If Math.Abs(fTestDelta) < 0.00001 Then

                                               oSketchPoint = oCircle.GetCenterPoint2

                                               fOriginX = oSketchPoint.X

                                               fOriginY = oSketchPoint.Y

                                            End If

                                         End If

                                      Next

                                   End If

                           

                           

                                   ' Get a collection of picture elements in the sketch before insertion of new image.

                                   cSketchPicturesBefore = New Collection

                                   oaSketchPicturesBefore = oSketch.GetSketchPictures

                                   If oaSketchPicturesBefore IsNot Nothing Then

                                      For Each oSketchPicture In oaSketchPicturesBefore

                                         cSketchPicturesBefore.Add(oSketchPicture.GetFeature.GetID)

                                      Next

                                   End If

                           

                                   ' Insert the datamatrix bitmap.

                                   oSketchManager.InsertSketchPicture(IMAGE_FILE_PATH)

                           

                                   ' Find the new picture element--it is not in the before collection.

                                   oaSketchPicturesAfter = oSketch.GetSketchPictures

                                   If oaSketchPicturesAfter IsNot Nothing Then

                                      For Each oSketchPicture In oaSketchPicturesAfter

                                         If Not cSketchPicturesBefore.Contains(oSketchPicture.GetFeature.GetID) Then

                                            ' Move and size the new picture element.

                                            oSketchPictureNew = oSketchPicture

                                            oSketchPictureNew.SetOrigin(fOriginX, fOriginY)

                                            oSketchPictureNew.SetSize(0.02, 0.02, True)  ' 20mm

                                            Exit For

                                         End If

                                      Next

                                   End If

                           

                                   oDrawingDoc.EditSheet()

                                   iSheetCur += 1

                                End While

                           

                                oSketch = Nothing

                                oSketchPicture = Nothing

                                oSketchPictureNew = Nothing

                                oaSketchPicturesAfter = Nothing

                                oaSketchPicturesBefore = Nothing

                                cSketchPicturesBefore = Nothing

                                oaSketchBlockInstances = Nothing

                                oSketchBlockInstance = Nothing

                                oView = Nothing

                             End Sub

                            • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                              John Alexander
                              While iSheetCur < iSheetCount

                               

                               

                              Maybe try (iSheetCount + 1) on the upper limit or <=. I say this only because you indicated that your previous approach wasn't working on the second sheet.

                               

                              You can also put a break point in your code right at the start of the loop so you can actually verify that it is checking the second sheet during runtime. You would just have to keep track of whether or not it encountered the breakpoint twice.

                               

                                       ' Insert the datamatrix bitmap.

                                       oSketchManager.InsertSketchPicture(IMAGE_FILE_PATH)

                              It looks like InsertSketchPicture returns a Picture object. You needn't search for it after inserting it.

                              Dim swSketchPicture as Sldworks.SketchPicture

                              set swSketchPicture = oSketchManager.InsertSketchPicture(IMAGE_FILE_PATH)

                              2012 SOLIDWORKS API Help - InsertSketchPicture Method (ISketchManager)

                               

                              Sort of on a tangent to your question, I'm not clear on your process/goal. Why do you need to explode the sketch blocks? I read in an earlier post that you want the center points of the circles to define the position of your images. Are the sketch blocks going to live on the drawing or be inserted programmatically? If you know where to put them programmatically, why can't you just insert the pictures straight away?

                               

                              In any case, couldn't you use the sketchblockinstance's insertion point/position instead of trying to explode it?

                                • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                  Greg Ashmore

                                  Seems that the operation did not work on the second sheet because I was not using the correct sketch. It does work when I set the sketch to the template sketch, not the view sketch.

                                   

                                  You are correct that SketchManager.InsertSketchPicture returns a reference to the picture. I had originally looked at ModelDoc2.InsertSketchPicture, which returns a boolean. I did not notice the difference in return value.

                                   

                                  The reason I am exploding the sketch block instances is that the sketch block defintion includes four lines that indicate (to the creator of the sheet format) where the datamatrix will be placed. These lines are deleted from the definition. The picture is actually placed at the original origin of the sketch block, entirely within the original bounds of the sketch block instance. I did not explain all that in the query because it is, as you say, a tangential issue. I explode the block instance to ensure that the original border of the sketch block will not obscure the new picture. This may not be necessary--but it is better to be sure than to find out a year from now that some pictures are not visible.

                                   

                                  Note to interested readers: The sketch block instance in a sheet format needs to be exploded while editing the sheet format for that sheet. You can't just retrieve the instances from the definition and explode them.

                        • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                          Josh Brady

                          The first view of a drawing sheet is the sheet itself.  That means any sketch entities, notes, etc that you drop onto the sheet that are not attached to a view.

                           

                          However, you said that you're looking for something that's in the sheet format, not on the sheet itself.

                           

                          To get the sketch that makes up the sheet format, use ISheet::GetTemplateSketch.  You should (I believe) be able to get your sketch block from there with GetSketchBlockInstances. 

                          • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                            Josh Brady

                            You can definitely get sketch block instances from a sketch, whether you're in a part, drawing, or assembly.  The snippet of the help you referenced refers to the best way to get the sketch block definition, not instances.

                             

                            Please examine this code to find the instances of sketch blocks contained in the sheet format of the current sheet.

                             

                            Dim swApp As SldWorks.SldWorks
                            Dim swDoc As SldWorks.ModelDoc2
                            Dim swDwg As SldWorks.DrawingDoc
                            Dim swSht As SldWorks.Sheet
                            Dim swSketch As SldWorks.Sketch
                            Dim swBlk As SldWorks.SketchBlockInstance
                            Dim swDef As SldWorks.SketchBlockDefinition
                            Dim nInst As Long
                            Dim vInstances As Variant
                            Dim i As Long
                            
                            
                            Sub main()
                            
                            
                            Set swApp = Application.SldWorks
                            Set swDoc = swApp.ActiveDoc
                            Set swDwg = swDoc
                            Set swSht = swDwg.GetCurrentSheet
                            Set swSketch = swSht.GetTemplateSketch
                            nInst = swSketch.GetSketchBlockInstanceCount
                            Debug.Print "Sketch block instances in sheet format: ", nInst
                            If nInst > 0 Then
                                vInstances = swSketch.GetSketchBlockInstances
                                For i = 0 To nInst - 1
                                    Set swBlk = vInstances(i)
                                    Debug.Print "Instance " & i + 1 & ":"
                                    Debug.Print , swBlk.Definition.FileName
                                    Debug.Print , swBlk.InstancePosition.ArrayData(0), swBlk.InstancePosition.ArrayData(1), swBlk.InstancePosition.ArrayData(2)
                                Next i
                            End If
                            End Sub
                            
                              • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                Alex Burnett

                                Josh Brady wrote:

                                 

                                Wow. So much wrong information in this thread!

                                 

                                John and Alex, you most certainly can get sketch block instances from a sketch, whether you're in a part, drawing, or assembly. The snippet of the help you referenced refers to the best way to get the sketch block definition, not instances.

                                 

                                I was actually just about to dive into testing whether or not you could approach it from the direction the OP was trying. I simply stated I wasn't sure if you could do it that way. Your information is quite helpful.

                                 

                                Do you know if there's a way to determine which sheet an instance is on? This sounds like it's the current roadblock.

                                  • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                    Josh Brady

                                    Alex Burnett wrote:

                                     

                                    Josh Brady wrote:

                                     

                                    Wow. So much wrong information in this thread!

                                     

                                    John and Alex, you most certainly can get sketch block instances from a sketch, whether you're in a part, drawing, or assembly. The snippet of the help you referenced refers to the best way to get the sketch block definition, not instances.

                                     

                                    I was actually just about to dive into testing whether or not you could approach it from the direction the OP was trying. I simply stated I wasn't sure if you could do it that way. Your information is quite helpful.

                                     

                                    Do you know if there's a way to determine which sheet an instance is on? This sounds like it's the current roadblock.

                                    Did you read my post?  Did you examine the code?

                                     

                                    Original post problem: How can I find instances of a given sketch block in the sheet format of a given sheet?

                                    Code I posted: Finds all sketch block instances in the sheet format of the active sheet, gets definition of each instance, posts location and definition path to the immediate window.  From there you can determine whether that instance matches the definition in question.

                                      • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                        Alex Burnett

                                        Josh Brady wrote:

                                         

                                        Alex Burnett wrote:

                                         

                                        Josh Brady wrote:

                                         

                                        Wow. So much wrong information in this thread!

                                         

                                        John and Alex, you most certainly can get sketch block instances from a sketch, whether you're in a part, drawing, or assembly. The snippet of the help you referenced refers to the best way to get the sketch block definition, not instances.

                                         

                                        I was actually just about to dive into testing whether or not you could approach it from the direction the OP was trying. I simply stated I wasn't sure if you could do it that way. Your information is quite helpful.

                                         

                                        Do you know if there's a way to determine which sheet an instance is on? This sounds like it's the current roadblock.

                                        Did you read my post? Did you examine the code?

                                         

                                        Original post problem: How can I find instances of a given sketch block in the sheet format of a given sheet?

                                        Code I posted: Finds all sketch block instances in the sheet format of the active sheet, gets definition of each instance, posts location and definition path to the immediate window. From there you can determine whether that instance matches the definition in question.

                                        I understand that you are working with the current sheet and I see what your code does. I suppose I was unclear with the wording of my question. It doesn't pertain to the code you posted specifically, rather I was wondering if there was a way to know which sheet an instance is on from the instance directly (it seems like there isn't from my research). However, Greg found a solution so I suppose it doesn't matter at this point.

                                          • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                            Josh Brady

                                            I was wondering if there was a way to know which sheet an instance is on from the instance directly (it seems like there isn't from my research). However, Greg found a solution so I suppose it doesn't matter at this point.

                                            Yup. There is.  See below.  The bit from "Set swOwnerSketch...." and beyond should work no matter how you got the SketchBlockInstance object.

                                             

                                            Dim swApp As SldWorks.SldWorks
                                            Dim swDoc As SldWorks.ModelDoc2
                                            Dim swDwg As SldWorks.DrawingDoc
                                            Dim swSht As SldWorks.Sheet
                                            Dim swSketch As SldWorks.Sketch
                                            Dim swOwnerSketch As SldWorks.Sketch
                                            Dim swBlk As SldWorks.SketchBlockInstance
                                            Dim swDef As SldWorks.SketchBlockDefinition
                                            Dim swFeat As SldWorks.Feature
                                            Dim nInst As Long
                                            Dim vInstances As Variant
                                            Dim i As Long
                                              
                                              
                                            Sub main()
                                              
                                              
                                            Set swApp = Application.SldWorks
                                            Set swDoc = swApp.ActiveDoc
                                            Set swDwg = swDoc
                                            Set swSht = swDwg.GetCurrentSheet
                                            Set swSketch = swSht.GetTemplateSketch
                                            nInst = swSketch.GetSketchBlockInstanceCount
                                            Debug.Print "Sketch block instances in sheet format: ", nInst
                                            If nInst > 0 Then
                                                vInstances = swSketch.GetSketchBlockInstances
                                                For i = 0 To nInst - 1
                                                    Set swBlk = vInstances(i)
                                                    Debug.Print "Instance " & i + 1 & ":"
                                                    Debug.Print , "Definition: ", swBlk.Definition.FileName
                                                    Debug.Print , "Position: ", swBlk.InstancePosition.ArrayData(0), swBlk.InstancePosition.ArrayData(1), swBlk.InstancePosition.ArrayData(2)
                                                    Debug.Print , "Owning feature heiarchy:"
                                                    Set swOwnerSketch = swBlk.GetSketch
                                                    Set swFeat = swOwnerSketch
                                                    While Not swFeat Is Nothing
                                                        Debug.Print , , swFeat.Name
                                                        Set swFeat = swFeat.GetOwnerFeature
                                                    Wend
                                                Next i
                                            End If
                                            End Sub
                                            
                                      • Re: Can't find the instance of a sketch block definition on a drawing sheet.
                                        Greg Ashmore

                                        As Josh Brady pointed out, the key is to get the sketch for the sheet format, as opposed to the sketch for the sheet itself.

                                        The sample code that Josh posted correctly shows how to get the sketch for the sheet format.

                                        With that method incorporated into my code, I am able to obtain a reference to the sketch block instance in each sheet and record its location.

                                        The updated code is shown below.

                                        I've marked this as the correct answer because it is production code; it does not write to the immediate window as in Josh's example.

                                        However, credit for the answer belongs to Josh.

                                         

                                        Thanks to all for your help.

                                         

                                           Private Shared Sub DatamatrixInstanceLocationsGet()

                                              Dim nInstanceSeen As Boolean

                                              Dim iSheetCur As Integer

                                              Dim sSheetName As String

                                              Dim oMathPoint As MathPoint

                                              Dim oSheet As Sheet

                                              Dim oSketch As Sketch

                                              Dim oaSketchBlockInstances() As Object

                                              Dim oSketchBlockInstance As SketchBlockInstance

                                         

                                              iSheetCur = 0

                                              While iSheetCur < iSheetCount

                                                 sSheetName = oaSheetNames(iSheetCur)

                                                 oDrawingModel.ActivateSheet(sSheetName)

                                                 oSheet = oDrawingDoc.GetCurrentSheet

                                                 oSketch = oSheet.GetTemplateSketch

                                         

                                                 nInstanceSeen = False

                                                 oaSketchBlockInstances = oSketch.GetSketchBlockInstances

                                                 If oaSketchBlockInstances IsNot Nothing Then

                                                    For Each oSketchBlockInstance In oaSketchBlockInstances

                                                       If oSketchBlockInstance.Definition.FileName = sDatamatrixBlockDefinitionFileName Then

                                                          oMathPoint = oSketchBlockInstance.InstancePosition

                                                          faDatamatrixOriginX(iSheetCur) = oMathPoint.ArrayData(0)

                                                          faDatamatrixOriginY(iSheetCur) = oMathPoint.ArrayData(1)

                                                          nInstanceSeen = True

                                                       End If

                                                    Next

                                                 End If

                                                 If Not nInstanceSeen Then

                                                    Throw New Exception("Datamatrix sketch block not found on sheet " & (iSheetCur + 1).ToString)

                                                 End If

                                         

                                                 iSheetCur += 1

                                              End While

                                         

                                         

                                              oMathPoint = Nothing

                                              oSheet = Nothing

                                              oSketch = Nothing

                                              oaSketchBlockInstances = Nothing

                                              oSketchBlockInstance = Nothing

                                           End Sub