'
'  (C) Copyright 2007 by Autodesk, Inc.  All rights reserved.
'
' Permission to use, copy, modify, and distribute this software in
' object code form for any purpose and without fee is hereby granted, 
' provided that the above copyright notice appears in all copies and 
' that both that copyright notice and the limited warranty and
' restricted rights notice below appear in all supporting 
' documentation.
'
' AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
' AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
' MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
' DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
' UNINTERRUPTED OR ERROR FREE.
'
' Use, duplication, or disclosure by the U.S. Government is subject to 
' restrictions set forth in FAR 52.227-19 (Commercial Computer
' Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
' (Rights in Technical Data and Computer Software), as applicable.
'
' This sample application shows how to create and edit scale area using COM API in SymBBAuto.
'

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Type

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common.AcRegenType
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Interop.Common

Imports SymBBAuto.McadOrientation
Imports SymBBAuto.McadWeldSymbolType
Imports SymBBAuto

<Assembly: ExtensionApplication(GetType(netScaleArea.netScaleArea))> 
<Assembly: CommandClass(GetType(netScaleArea.CreateCommand))> 
Namespace netScaleArea
    Public Class netScaleArea
        Implements Autodesk.AutoCAD.Runtime.IExtensionApplication

        ' Declaration of global variables
        Dim symbb As McadSymbolBBMgr
        Dim acadApp As AcadApplication = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18")
        Dim ThisDrawing As AcadDocument = acadApp.ActiveDocument

        Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize

        End Sub

        Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate

        End Sub

        Public Sub CreateScaleArea()
            symbb = ThisDrawing.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr")

            'Make sure we select scale area in the model space
            If ThisDrawing.ActiveSpace = AcActiveSpace.acPaperSpace Then
                ThisDrawing.ActiveSpace = AcActiveSpace.acModelSpace
            End If

            Dim closedCurveId As Long
            Dim objIds(1) As Object

            Dim plineEnt As AcadLWPolyline
            Dim dblVertices() As Double

            'setup initial points for Polyline
            ReDim dblVertices(7)
            dblVertices(0) = 1 : dblVertices(1) = 2
            dblVertices(2) = 2 : dblVertices(3) = 2
            dblVertices(4) = 2 : dblVertices(5) = 1
            dblVertices(6) = 1 : dblVertices(7) = 1

            'Draw Polyline and get its ID
            plineEnt = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblVertices)
            plineEnt.Closed = True
            closedCurveId = plineEnt.ObjectID
            plineEnt.Update()

            Dim varCenter(2) As Double
            Dim dblRadius As Double
            Dim cleEnt As AcadCircle

            varCenter(0) = 1.4 : varCenter(1) = 1.3 : varCenter(2) = 0
            dblRadius = 0.5

            'draw the Circle
            cleEnt = ThisDrawing.ModelSpace.AddCircle(varCenter, dblRadius)
            objIds(0) = cleEnt.ObjectID
            cleEnt.Update()

            Dim varStart(2) As Double
            Dim varEnd(2) As Double
            Dim lineEnt As AcadLine

            varStart(0) = 1.3 : varStart(1) = 1.3 : varStart(2) = 0
            varEnd(0) = 1.7 : varEnd(1) = 1.7 : varEnd(2) = 0

            'Draw the Line
            lineEnt = ThisDrawing.ModelSpace.AddLine(varStart, varEnd)
            objIds(1) = lineEnt.ObjectID
            lineEnt.Update()

            'Get the scale area manager object
            Dim samgr As McadScaleAreaMgr = symbb.ScaleAreaMgr

            'Create scale area object by attached object ID
            Dim sa As McadScaleArea = samgr.CreateScaleArea(closedCurveId)
            If sa Is Nothing Then
                MsgBox("Fail to create scale area")
                Exit Sub
            End If

            'Modify properties of scale area
            sa.ScaleType = McadScaleType.sbScalePf
            sa.TextScalingFactor = 1

            sa.SetBasicScale()

            'rescale selected objects
            If VarType(objIds(0)) = VariantType.Integer Then
                'In 32 bit OS, data type of ObjectID is Integer
                Dim objIdsInt(1) As Integer
                objIdsInt(0) = objIds(0)
                objIdsInt(1) = objIds(1)
                sa.RescaleObjects(objIdsInt)
            ElseIf VarType(objIds(0)) = VariantType.Long Then
                'In 64 bit OS, data type of ObjectID is Long
                Dim objIdsLong(1) As Long
                objIdsLong(0) = objIds(0)
                objIdsLong(1) = objIds(1)
                sa.RescaleObjects(objIdsLong)
            End If

            'Create viewport with specified position in the specified layout
            Dim viewPos(2) As Double
            viewPos(0) = 4 : viewPos(1) = 4 : viewPos(2) = 0
            sa.CreateViewPort("Layout1", viewPos)

            ThisDrawing.Regen(acAllViewports)
        End Sub

        Public Sub EditScaleArea()
            symbb = ThisDrawing.Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr")

            'Make sure we select scale area in the model space
            If ThisDrawing.ActiveSpace = AcActiveSpace.acPaperSpace Then
                ThisDrawing.ActiveSpace = AcActiveSpace.acModelSpace
            End If

            'Allow user to pick on a object in the model space
            Dim objDrawingObject As AcadEntity = Nothing
            On Error Resume Next
            ThisDrawing.Utility.GetEntity(objDrawingObject, Nothing, "Select scale area to modify:")
            If objDrawingObject Is Nothing Then
                MsgBox("You did not pick an object")
                Exit Sub
            End If

            Dim borderId As Long
            borderId = objDrawingObject.ObjectID

            'Get the scale area manager object
            Dim samgr As McadScaleAreaMgr = symbb.ScaleAreaMgr

            'Get scale area object by specified scale area ID
            Dim sa As McadScaleArea = samgr.FindScaleArea(borderId, False)
            If sa Is Nothing Then
                MsgBox("Fail to find scale area")
                Exit Sub
            End If

            'Update scale type and reset scale value
            sa.ScaleType = McadScaleType.sbScaleDf
            sa.LengthScalingFactor = 0.5

            'Delete attached viewport
            sa.DeleteViewPort()

            'Move scale area
            Dim newPos(2) As Double
            newPos(0) = 3 : newPos(1) = 3 : newPos(2) = 0
            sa.MoveBorder(newPos)

            'If uncommenting the below, you can delete scale area as you need
            'samgr.DeleteScaleArea(sa)

            ThisDrawing.Regen(acAllViewports)
        End Sub
    End Class

    Public Class createCommand

        ' Declaration of global variables
        Dim sa As New netScaleArea

        <Autodesk.AutoCAD.Runtime.CommandMethod("CREATESCAREA")> _
        Public Sub CreateScaleArea()
            ' Create Scale Area
            sa.CreateScaleArea()
        End Sub

        <Autodesk.AutoCAD.Runtime.CommandMethod("MODIFYSCAREA")> _
        Public Sub ModifyScaleArea()
            ' Modify scale area
            sa.EditScaleArea()
        End Sub
    End Class

End Namespace

