VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' step10 -    

' 1. C                - TypeAttrBolt
' 2. C         - CreateDet
' 3. C                - CreateStandart
' 4.     - DecomposeSpc
' 5.                  - ShowSpc

Dim iKompasObject As Kompas6API5.Application  '  KompasObject
Dim doc As Kompas6API5.Document2D             '  ksDocument2D
Dim specActiv As Kompas6API5.SpcDocument      '  ksSpcDocument
Dim iDocument2D As Kompas6API5.Document2D     '  ksDocument2D
Dim spec As Kompas6API5.Specification         '  ksSpecification

Const SPC_NAME = 5

' C     ""

Function EditSpcObjDet(geom As Long) As Long
  Dim spcObj As Long
  spcObj = 0
  '     ,      
  If iDocument2D.ksEditMacroMode Then
    '          
    spcObj = spec.ksGetSpcObjForGeom("graphic.lyt", 1, 0, 1, 1)
    '    
    If spec.ksSpcObjectEdit(spcObj) = 0 Then
      spcObj = 0
    End If
  End If

  '   ,      
  If spcObj Or spec.ksSpcObjectCreate("graphic.lyt", 1, 20, 0, 0, 0) Then
    Dim iPar As Kompas6API5.UserParam         '  ksUserParam
    '    
    Set iPar = iKompasObject.GetParamStruct(ko_UserParam)
    
    Dim iItem As Kompas6API5.LtVariant        '  ksLtVariant
    '       
    Set iItem = iKompasObject.GetParamStruct(ko_LtVariant)
    
    Dim iArr As Kompas6API5.DynamicArray      '  ksDynamicArray
    '     
    Set iArr = iKompasObject.GetDynamicArray(LTVARIANT_ARR)
    
    '  -      -   
    If iPar Is Nothing Or iItem Is Nothing Or iArr Is Nothing Then
      EditSpcObjDet = 0
      Exit Function
    End If
    
    iPar.Init                                 '   
    iPar.SetUserArray iArr                    '   
    iItem.Init                                '       
    iItem.strVal = ""                   '    -  ""
    iArr.ksAddArrayItem -1, iItem             '    
    
    '    - 
    spec.ksSpcChangeValue 5, 1, iPar, STRING_ATTR_TYPE

    '  
    If geom Then
      spec.ksSpcIncludeReference geom, 1
    End If

    spcObj = spec.ksSpcObjectEnd()            '    
    '           
    ' .     Cursor  Placement
    If spcObj Then
      '   ,   
      '   ,    
      spec.ksEditWindowSpcObject (spcObj)
      EditSpcObjDet = spcObj
      Exit Function
    End If
  End If
  EditSpcObjDet = 0
End Function

'    
'  ,    
'     Cursor  Placement

Sub DrawPosLeader(spcObj As Long)
  Dim iInfo As Kompas6API5.RequestInfo        '  ksRequestInfo
  '      
  Set iInfo = iKompasObject.GetParamStruct(ko_RequestInfo)
  '      -   
  If iInfo Is Nothing Then
    Exit Sub
  End If
  
  iInfo.Init                                  '      
  Dim flag As Boolean                         '    
  flag = False
  Dim posLeater  As Long                      '   
  posLeater = 0
  Dim x1 As Double, y1 As Double              '   
  
  Do
    '      
    iInfo.commandsString = "!    !  "
    ' C   
    iInfo.prompt = "   "
    Dim j1 As Integer
    '     
    j1 = iDocument2D.ksCursor(iInfo, x1, y1, Nothing)
    Select Case j1
      Case 1:                                 '    
        posLeater = iDocument2D.ksCreateViewObject(POSLEADER_OBJ)
        flag = False
      Case 2:                                 '  
        '      
        iInfo.prompt = "  "
        '     
        If iDocument2D.ksCursor(iInfo, x1, y1, Nothing) Then
          '       
          '   -   (x1, y1) = 100
          posLeater = iDocument2D.ksFindObj(x1, y1, 100)
          '     
          If posLeater = 0 Or iDocument2D.ksGetObjParam(posLeater, Nothing, 0) <> POSLEADER_OBJ Then
             iKompasObject.ksError "!     !"
             posLeater = 0
             flag = True
          Else
            flag = False
          End If
        Else
          flag = False
        End If
      Case -1:
        '       
        '   -   (x1, y1) = 100
        posLeater = iDocument2D.ksFindObj(x1, y1, 100)
        '     
        If posLeater = 0 Or iDocument2D.ksGetObjParam(posLeater, Nothing, 0) <> POSLEADER_OBJ Then
           iKompasObject.ksError "!     !"
           posLeater = 0
           flag = True
        Else
          flag = False
        End If
    End Select
  Loop Until flag = False
  
  '   ,     
  If posLeater Then
    '          
    If spec.ksSpcObjectEdit(spcObj) Then      '     
      '      -   
      ' posLeater -     ;
      ' 1 -   , 0 -   
      spec.ksSpcIncludeReference posLeater, True
      '    
      spec.ksSpcObjectEnd
    End If
  End If
End Sub

' C   

Sub TypeAttrBolt()
  Dim iattr As Kompas6API5.AttributeObject    '  ksAttributeObject
  '      .
  Set iattr = iKompasObject.GetAttributeObject()
  
  Dim itype As Kompas6API5.AttributeTypeParam '  ksAttributeTypeParam
  '      
  Set itype = iKompasObject.GetParamStruct(ko_AttributeType)
  
  Dim icol As Kompas6API5.ColumnInfoParam     '  ksColumnInfoParam
  '      
  Set icol = iKompasObject.GetParamStruct(ko_ColumnInfoParam)
  
  '  -      -   
  If Not itype Is Nothing And Not icol Is Nothing And Not iattr Is Nothing Then
    itype.Init                                '     
    icol.Init
    itype.header = "111"                  ' o- 
    itype.rowsCount = 1                       ' -   
    itype.flagVisible = True                  ' ,     
    itype.password = ""                       ' ,      -  
                                              '   
    itype.key1 = 10                           '    
    itype.key2 = 20                           '    
    itype.key3 = 30                           '    
    itype.key4 = 0                            '   
    
    Dim iArr As Kompas6API5.DynamicArray      '  ksDynamicArray
    Set iArr = itype.GetColumns()             '    (  )
    '      -   
    If Not iArr Is Nothing Then
      '  1 " ."
      icol.header = " ."               ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 1                            '  ,  
                                              '       ( )
      icol.def = ""                       '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  2 ""
      icol.header = ""              ' o- 
      icol.Type = UINT_ATTR_TYPE              '     - <ldefin2d.bas>
      icol.Key = 3                            '  ,  
                                              '       ( )
      icol.def = "1"                          '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  3 ""
      icol.header = ""                  ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "M"                          '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  4 ""
      icol.header = ""                 ' o- 
      icol.Type = UINT_ATTR_TYPE              '     - <ldefin2d.bas>
      icol.Key = 3                            '  ,        
                                              '       ( )
      icol.def = "12"                         '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  5 ""
      icol.header = ""                        ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "x"                          '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  6 ""
      icol.header = ""                     ' o- 
      icol.Type = FLOAT_ATTR_TYPE             '     - <ldefin2d.bas>
      icol.Key = 3                            '  ,        
                                              '       ( )
      icol.def = "1.25"                       '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  7 " "
      icol.header = " "            ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 3                            '  ,        
                                              '       ( )
      icol.def = "-6g"                        '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  8 ""
      icol.header = ""                        ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "x"                          '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  9 ""
      icol.header = ""                   ' o- 
      icol.Type = UINT_ATTR_TYPE              '     - <ldefin2d.bas>
      icol.Key = 3                            '  ,        
                                              '       ( )
      icol.def = "60"                         '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  10 ". "
      icol.header = ". "           ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "58"                         '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  11 ""
      icol.header = ""                ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = ".35"                       '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  12 ""
      icol.header = ""                ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = ".16"                        '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  13 ""
      icol.header = ""                    ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = ""                       '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  14 ""
      icol.header = ""                   ' o- 
      icol.Type = UINT_ATTR_TYPE              '     - <ldefin2d.bas>
      icol.Key = 2                            '  ,        
                                              '       ( )
      icol.def = "7808"                       '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  15 ""
      icol.header = ""                        ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "-"                          '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    

      '  16 ""
      icol.header = ""                     ' o- 
      icol.Type = STRING_ATTR_TYPE            '     - <ldefin2d.bas>
      icol.Key = 0                            '  ,        
                                              '       ( )
      icol.def = "70"                         '   
      icol.flagEnum = False                   '   ,    
                                              '       1  0 
      iArr.ksAddArrayItem -1, icol            '    
    End If
    
    '   
    Dim numbType As Double
    numbType = iattr.ksCreateAttrType(itype, iKompasObject.ksChoiceFile("*.lat", "", False))
    If numbType Then
      iKompasObject.ksMessage "   = " & numbType
    End If
    '   
    iArr.ksDeleteArray
  End If
End Sub

' C   

Sub CreateDet()
  Dim gr As Long                              '   
  '   , type -   ( 0 -  , 1 -  )
  gr = iDocument2D.ksNewGroup(0)
    iDocument2D.ksLineSeg 20, 30, 70, 30, 2
    iDocument2D.ksLineSeg 70, 30, 70, 80, 2
    iDocument2D.ksLineSeg 70, 80, 20, 80, 2
    iDocument2D.ksLineSeg 20, 80, 20, 30, 2
  iDocument2D.ksEndGroup                      '    
  
  '   
  Dim spcObj As Long
  spcObj = EditSpcObjDet(gr)                  ' C     ""
  
  If spcObj Then                              '   
    DrawPosLeader spcObj                      '    
  End If
End Sub

'   

Sub CreateStandart()
  Dim spcObj As Long
  spcObj = 0
  
  '     ,      
  If iDocument2D.ksEditMacroMode Then
    '          
    spcObj = spec.ksGetSpcObjForGeom("graphic.lyt", 1, 0, 1, 1)
    '    
    If spec.ksSpcObjectEdit(spcObj) = 0 Then
      spcObj = 0
    End If
  End If
  
  '   ,      
  If spcObj Or spec.ksSpcObjectCreate("graphic.lyt", 1, 25, 0, 313277777065#, 0) Then
    Dim iPar As Kompas6API5.UserParam         '  ksUserParam
    '    
    Set iPar = iKompasObject.GetParamStruct(ko_UserParam)
    
    Dim iItem As Kompas6API5.LtVariant        '  ksLtVariant
    '       
    Set iItem = iKompasObject.GetParamStruct(ko_LtVariant)
    
    Dim iArr As Kompas6API5.DynamicArray      '  ksDynamicArray
    '     
    Set iArr = iKompasObject.GetDynamicArray(LTVARIANT_ARR)
    
    '  -      -   
    If iPar Is Nothing Or iItem Is Nothing Or iArr Is Nothing Then
      Exit Sub
    End If
    
    iPar.Init                                 '   
    iPar.SetUserArray iArr                    '   
    iItem.Init                                '       
    iItem.strVal = "111"                  '    -  "111"
    iArr.ksAddArrayItem -1, iItem             '    
    spec.ksSpcChangeValue 5, 1, iPar, STRING_ATTR_TYPE  '    - 
    
    spec.ksSpcVisible SPC_NAME, 2, 1          '  
    
    iArr.ksClearArray                         '  
    iItem.Init                                '   
    Dim uBuf As Integer                       '  
    uBuf = 2                                  ' 
    iItem.intVal = uBuf                       '    -  2
    iArr.ksAddArrayItem -1, iItem             '    
    spec.ksSpcChangeValue SPC_NAME, 2, iPar, UINT_ATTR_TYPE '    - 

    '  
    iArr.ksClearArray                         '  
    iItem.Init                                '   
    iItem.doubleVal = 40                      '  
    iArr.ksAddArrayItem -1, iItem             '    
    spec.ksSpcChangeValue SPC_NAME, 4, iPar, DOUBLE_ATTR_TYPE '    - 

    '  
    iArr.ksClearArray                         '  
    iItem.Init                                '   
    iItem.floatVal = 1
    iArr.ksAddArrayItem -1, iItem             '    
    
    spec.ksSpcVisible SPC_NAME, 5, 1          '     
    spec.ksSpcVisible SPC_NAME, 6, 1
    spec.ksSpcChangeValue SPC_NAME, 6, iPar, FLOAT_ATTR_TYPE  '    - 

    '   
    spec.ksSpcVisible SPC_NAME, 7, 0

    '  
    iArr.ksClearArray                         '  
    iItem.Init                                '   
    iItem.intVal = 55                         '  
    iArr.ksAddArrayItem -1, iItem             '    
    spec.ksSpcChangeValue SPC_NAME, 9, iPar, UINT_ATTR_TYPE '    -  

    spec.ksSpcVisible SPC_NAME, 10, 0         '   
    spec.ksSpcVisible SPC_NAME, 11, 0         '  
    spec.ksSpcVisible SPC_NAME, 12, 0         '  

    '  
    iArr.ksClearArray                         '  
    iItem.Init                                '   
    iItem.floatVal = 7808                     ' 
    iArr.ksAddArrayItem -1, iItem             '    
    spec.ksSpcChangeValue SPC_NAME, 14, iPar, UINT_ATTR_TYPE  '   - 

    '  
    If geom Then
      spec.ksSpcIncludeReference geom, 1
    End If

    spcObj = spec.ksSpcObjectEnd              '    
    '    ,       
    ' .      Cursor  Placement
    If spcObj Or spec.ksEditWindowSpcObject(spcObj) Then
      '   ,   
      '   ,    
      DrawPosLeader spcObj
    End If
 End If
End Sub

'    

Sub DecomposeSpc()
  Dim pSpc As Long                            '    
  pSpc = specActiv.reference                  '    
  If pSpc = 0 Then                            '    -   
    Exit Sub
  End If
  
  '    
  Dim pageCount As Integer
  pageCount = specActiv.ksGetSpcDocumentPagesCount
  
  Dim ispcGabarit As Kompas6API5.RectParam    '  ksRectParam
  '       
  Set ispcGabarit = iKompasObject.GetParamStruct(ko_RectParam)
  If ispcGabarit Is Nothing Then              '      -   
    Exit Sub
  End If
  
  doc.ksGetObjGabaritRect pSpc, ispcGabarit   '     

  '  
  Dim idocPar As Kompas6API5.DocumentParam    '  ksDocumentParam
  '    
  Set idocPar = iKompasObject.GetParamStruct(ko_DocumentParam)
  If idocPar Is Nothing Then                  '      -   
    Exit Sub
  End If
  
  idocPar.Init                                '  
  idocPar.regime = 0                          '   - 
  idocPar.Type = lt_DocFragment               '   - 
  doc.ksCreateDocument idocPar                '  
  
  For i = 0 To pageCount - 1
    Dim group As Long
    group = doc.ksDecomposeObj(pSpc, 0, 0.4, i + 1) '      
    
    If group Then                             '    i-  
      Dim imathBop As Kompas6API5.MathPointParam  '  ksMathPointParam
      Set imathBop = ispcGabarit.GetpBot          '      
      Dim imathTop As Kompas6API5.MathPointParam  '  ksMathPointParam
      Set imathTop = ispcGabarit.GetpTop          '      
      
      '  
      Dim column As Integer
      column = i Mod 3                            ' i%3
      
      Dim x As Double
      x = (imathTop.x - imathBop.x + 5) * column
      
      Dim row As Integer
      row = i \ 3
      
      Dim y As Double
      y = (imathTop.y - imathBop.y + 5) * row
      
      doc.ksMoveObj group, x, -y              ' C 
      doc.ksStoreTmpGroup group               '     
      doc.ksClearGroup group, 1               '   
      doc.ksDeleteObj group                   '  
    End If
  Next
  iKompasObject.ksMessage " = " & pageCount & "  "
End Sub

'   

Sub ShowSpc()
  Dim iIter As Kompas6API5.Iterator           '  ksIterator
  Set iIter = iKompasObject.GetIterator()     '   
  
  If Not iIter Is Nothing Then                '  
    '       
    '      ,     ,   ,
    '        .
    '  -  nameLib  styleNumb   ( ..      
    '  ,     )
    iIter.ksCreateSpcIterator 0, 0, 0         '
    
    If iIter.reference Then                   '  
      Dim obj As Long                         '   
      obj = iIter.ksMoveIterator("F")         '     
      
      If specActiv.ksExistObj(obj) Then       '   
        
        Do
          '       
          Dim count As Integer
          count = spec.ksGetSpcTableColumn(0, 0, 0)
          iKompasObject.ksMessage "-  = " & count
      
          For i = 1 To count                  '    
            '      ,    
            Dim ispcColPar As Kompas6API5.SpcColumnParam '  ksSpcColumnParam
            '     
            Set ispcColPar = iKompasObject.GetParamStruct(ko_SpcColumnParam)
            
            '          ,
            '    ,  .
            If spec.ksGetSpcColumnType(obj, i, ispcColPar) Then
              
              '  
              Dim columnType As Long          '  
              columnType = ispcColPar.columnType
              Dim ispoln As Long              '    
              ispoln = ispcColPar.ispoln
              Dim blok As Long                '  ,   0
              blok = ispcColPar.block
              Dim buf As String               '   ,      
              '       
              buf = spec.ksGetSpcObjectColumnText(obj, columnType, ispoln, blok)
              '             
              Dim colNumb As Integer
              colNumb = spec.ksGetSpcColumnNumb(obj, ispcColPar.columnType, ispcColPar.ispoln, ispcColPar.block)
              
              iKompasObject.ksMessage " text = " & buf & ",columnType = " & columnType & ", ispoln = " & ispoln _
                & ", block = " & blok & ", i = " & i & ", colNumb = " & colNumb
            End If
          Next
          
          obj = iIter.ksMoveIterator("N")     '     
        Loop Until obj = 0
      End If
      
      iIter.ksDeleteIterator                  '      
    End If
  End If
End Sub

'   

Public Function GetLibraryName() As String
  GetLibraryName = "H  "      '  
End Function

'   

Public Function ExternalMenuItem(ByVal number As Integer, itemType As Integer, command As Integer) As String
  itemType = 1 'MENUITEM'
  
  Select Case number
    Case 1                                            '  1 -    
      ExternalMenuItem = "   "
      command = 1
    Case 2                                            '  2 -     
      ExternalMenuItem = "    "
      command = 2
    Case 3                                            '  3 -      
      ExternalMenuItem = "     "
      command = 3
    Case 4                                            '  4 -    
      ExternalMenuItem = "   "
      command = 4
    Case 5                                            '  5 -   
      ExternalMenuItem = "  "
      command = 5
    Case 6                                            '   
      itemType = 3 'ENDMENU'
      ExternalMenuItem = ""
      command = -1
  End Select
End Function

'    -      

Public Sub ExternalRunCommand(ByVal command As Integer, ByVal mode As Integer, ByVal Kompas As Kompas6API5.Application)
  Set iKompasObject = Kompas                          '   
  
  If iKompasObject Is Nothing Then                    '     - 
    Exit Sub                                          '    
  End If
  
  If command = 1 Then                                 '  1
    TypeAttrBolt                                      '    
  End If
  
  If command = 2 Or command = 3 Then                  '  2   3
    Set iDocument2D = iKompasObject.ActiveDocument2D  '    2D 
    
    If iDocument2D Is Nothing Then                    '       2D 
      Exit Sub                                        '      
    End If
    
    Set spec = iDocument2D.GetSpecification           '   
    
    If spec Is Nothing Then                           '     
      Exit Sub                                        '      
    End If
    
    Select Case command
      Case 2
        CreateDet                                     '     
      Case 3
        CreateStandart                                '      
    End Select
  End If
  
  
  If command = 4 Or command = 5 Then                  '  4   5
    
    Set doc = iKompasObject.Document2D                '   2D 
    
    Set specActiv = iKompasObject.SpcActiveDocument   '     
    If doc Is Nothing Or specActiv Is Nothing Then    '        
      Exit Sub                                        '      
    End If
    
    Set spec = specActiv.GetSpecification             '   
    
    If spec Is Nothing Then                           '     
      Exit Sub                                        '      
    End If
    
    Select Case command
      Case 4
        DecomposeSpc                                  '    
      Case 5
        ShowSpc                                       '   
    End Select
  End If
  
  iKompasObject.ksMessageBoxResult                    '   
  
End Sub

