# coding: utf-8
#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#

# Author: Larry Pearce
# Revised by: Larry Pearce
# Revision History
# Date         Change                                 By
# 2020-10-12   Original code (1.0)                    Larry Pearce

import apex
from apex.construct import Point3D, Point2D


def doSketch(part, _plane, dim1, dim2):
   print("in sketch")
   sketch_1 = part.createSketchOnGlobalPlane(
        name = 'Sketch 1',
        plane = _plane,
        alignSketchViewWithViewport = True)

   rectangle_1 = sketch_1.createRectangle2Point(
        name = "Rectangle 1",
        location = Point2D( 0.0, 0.0 ),
        diagonal = Point2D( dim1, dim2 ))

   return sketch_1.completeSketch( fillSketches = True )


def main(dict={}):
   #apex.disableShowOutput()

   #apex.setScriptUnitSystem(unitSystemName = r'''in-slinch-s-lbf''')
   myModel = apex.currentModel()
   
   if dict["plane"] == "XY": plane = apex.construct.GlobalPlane.XY
   if dict["plane"] == "YZ": plane = apex.construct.GlobalPlane.YZ
   if dict["plane"] == "ZX": plane = apex.construct.GlobalPlane.ZX
   
   dim1 = float(dict["dim1"])
   dim2 = float(dict["dim2"])
   
   if dim1 <= 0.0: dim1 = 1.0
   if dim2 <= 0.0: dim2 = 1.0
   
   parts = myModel.getParts(True)
   if parts.len() == 0:
      myModel.createPart()
   
   part = myModel.getCurrentPart()
   
   doSketch(part, plane, dim1, dim2)
   




