##  [PyMotorCAD Cheat Sheet](/blog/pymotorcad-cheat-sheet) 

This is a quick-reference guide for [PyMotorCAD](https://motorcad.docs.pyansys.com/version/stable/). You can jump to a section of your choosing by using these links:

[Launching Motor-CAD using PyMotorCAD](#launch) | [Geometric properties](#geoprop) | [Material assignment](#matassign) | [Model motor](#model) | [E-magnetic performance curves in LAB](#lab) | [Operating point calculation](#oppoint) | [Electromagnetic calculations in Emag](#calc) | [Results](#results) | [PyMotorCAD scripting in MATLAB](#matlab) | [Backwards compatibility](#backcomp)

# <a name="launch"></a>LaunchMotor-CADusing PyMotorCAD

Launch a Motor-CAD instance locally and then exit: ```python
import ansys.motorcad.core as pymotorcad

mcad = pymotorcad.MotorCAD()
mcad.load_from_file(r"path/motor_cad.mot")

# Load predefined template
mcad.load_template(template_name)

# saving file to working_folder
mcad.save_to_file(
   os.path.join(working_folder,mcad_name+".mot")
)

# Exit MotorCAD
mcad.quit()

```

# <a name="geoprop"></a>Geometry properties

Specify the motor geometry: ```python
mcad.set_variable("Slot_Number", 24)
mcad.set_variable("Tooth_Width", 6)
mcad.set_variable("Magnet_Thickness", 4.5)
mcad.set_variable("Pole_Number", 4)

```

Set winding patterns:

```python
mcad.set_variable("MagWindingType", 1)
set_variable("MagTurnsConductor", 12)

```

# <a name="matassign"></a>Material assignment

Set stater and rotor lamination materials: ```python
mcad.set_component_material("Stator Lam (Back Iron)",
    "M250-35A")
mcad.set_component_material("Rotor Lam (Back Iron)",
    "M250-35A")

```

# <a name="model"></a>Model motor

Set building commands: ```python
# Build options
mcad.set_variable("ModelType_MotorLAB", 1)
mcad.set_variable("SatModelPoints_MotorLAB", 0)
mcad.set_variable("LossModel_Lab", 0)
mcad.set_variable("ModelBuildSpeed_MotorLAB", 10000)
mcad.set_variable("MaxModelCurrent_MotorLAB", 480)
mcad.set_variable("BuildSatModel_MotorLAB", True)

# Set lab context and build model
mcad.set_motorlab_context()
mcad.clear_model_build_lab()
mcad.build_model_lab()

```

# <a name="lab"></a>E-magnetic performance curves in LAB

Set lab operating mode: ```python
mcad.set_variable("OperatingMode_Lab", 0)

# setting magnetic calculation options
mcad.set_variable("EmagneticCalcType_Lab", 0)
mcad.set_variable("SpeedMax_MotorLAB", 10000)
mcad.set_variable("Speedinc_MotorLAB", 250)
mcad.set_variable("SpeedMin_MotorLAB", 500)
mcad.set_variable("Imax_MotorLAB", 480)

```

Calculate the E-Magnetic performance:

```python
mcad.calculate_magnetic_lab()

```

# <a name="oppoint"></a>Operating point calculation

Set the operating point variables: ```python
mcad.set_variable("OpPointSpec_MotorLAB", 1)
mcad.set_variable("StatorCurrentDemand_Lab", 480)
mcad.set_variable("SpeedDemand_MotorLAB", 4000)
mcad.set_variable("LabThermalCoupling", 0)
mcad.set_variable("LabMagneticCoupling", 0)

```

Calculate the operating point:

```python
mcad.calculate_operating_point_lab()

```

# <a name="calc"></a>Electromagnetic calculations in Emag

Perform electromagnetic calculations: ```python
# Set the torque calculation options
points_per_cycle = 60
number_cycles = 1 
mcad.set_variable("TorquePointsPerCycle", points_per_cycle)
mcad.set_variable("TorqueNumberCycles", number_cycles)

# Disable all performance tests except the ones for transient torque
mcad.set_variable("BackEMFCalculation", False)
mcad.set_variable("CoggingTorqueCalculation", False)

# Enable transient torque
mcad.set_variable("TorqueCalculation", True)

# Run Emag performance tests
mcad.do_magnetic_calculation()

```

# <a name="results"></a>Results

```python
# Get the transient torque waveform in Emag
for n in range(points_per_cycle):
    (x, y) = mcad.get_magnetic_graph_point("TorqueVW", n)
    rotor_position.append(x)
    torque_.append(y)

# Calculate line voltage 
line_voltage = mcad.get_variable("PeakLineLineVoltage")

# Retrieve lab performance curves
data = io.loadmat(
    os.path.join(
        working_folder,mcad_name,"Lab",
            "MotorLAB_elecdata.mat"
    )
 )
speed = data["Speed"]
shaft_torque = data["Shaft_Torque"]
shaft_power = data["Shaft_Power"]

```

# <a name="matlab"></a>PyMotorCAD scripting in MATLAB

The Python version that MATLAB is using has the ansys.motorcad.core package installed. PyMotorCAD is available to use in MATLAB. Import the ansys.motorcad.core Python package for use in MATLAB:

```python
pymotorcad = py.importlib.import_module(
    "ansys.motorcad.core")

```

# <a name="backcomp"></a>Backwards compatibility

Use ActiveX scripts to connect to MotorCAD: ```python
importwin32com.client

mcad = win32com.client.Dispatch("MotorCAD.AppAutomation")

```

Use PyMotorCAD to connect to MotorCAD:

```python
import ansys.motorcad.core as pymotorcad

mcad = pymotorcad.MotorCADCompatibility()

```

# <a name="references"></a>References from PyMotorCAD documentation

- [Getting started](https://motorcad.docs.pyansys.com/version/stable/getting_started/index.html)
- [Examples](https://motorcad.docs.pyansys.com/version/stable/examples/index.html)
- [API reference](https://motorcad.docs.pyansys.com/version/stable/methods/index.html)