<commit_before>﻿/*
Copyright (c) 2018, <NAME>, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using ClipperLib;
using MatterHackers.Agg.Transform;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters2D;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DesignTools.Operations;
using MatterHackers.VectorMath;
using Newtonsoft.Json;

namespace MatterHackers.MatterControl.DesignTools
{
	using Polygon = List<IntPoint>;
	using Polygons = List<List<IntPoint>>;
	public enum BaseTypes { None, Rectangle, Circle, /* Oval, Frame,*/ Outline, };

	public class BaseObject3D : Object3D, IPublicPropertyObject, IPropertyGridModifier
	{
		readonly double scalingForClipper = 1000;

		public BaseObject3D()
		{
			Name = "Base".Localize();
		}

		public override bool CanRemove => true;
		public override bool CanApply => true;

		public BaseTypes CurrentBaseType { get; set; }
		public double BaseSize { get; set; } = 3;
		public double InfillAmount { get; set; } = 3;
		public double ExtrusionHeight { get; set; } = 5;

		public override void Apply(UndoBuffer undoBuffer)
		{
			OperationSource.Apply(this);

			base.Apply(undoBuffer);
		}

		public override void Remove(UndoBuffer undoBuffer)
		{
			OperationSource.Remove(this);

			base.Remove(undoBuffer);
		}

		[JsonIgnore]
		public IVertexSource VertexSource
		{
			get
			{
				var vertexSource = (IPathObject)this.Descendants<IObject3D>().FirstOrDefault((i) => i is IPathObject);
				return vertexSource?.VertexSource;
			}
			set
			{
				var vertexSource = this.Children.OfType<IPathObject>().FirstOrDefault();
				if (vertexSource != null)
				{
					vertexSource.VertexSource = value;
				}
			}
		}

		public static BaseObject3D Create()
		{
			var item = new BaseObject3D();
			item.Rebuild(null);
			return item;
		}

		public override void OnInvalidate(InvalidateArgs invalidateType)
		{
			if ((invalidateType.InvalidateType == InvalidateType.Content
				|| invalidateType.InvalidateType == InvalidateType.Matrix
				|| invalidateType.InvalidateType == InvalidateType.Path
				|| invalidateType.InvalidateType == InvalidateType.Mesh)
				&& invalidateType.Source != this
				&& !RebuildSuspended)
			{
				Rebuild(null);
			}
			else
			{
				base.OnInvalidate(invalidateType);
			}
		}

		public override void Rebuild(UndoBuffer undoBuffer)
		{
			this.DebugDepth("Rebuild");

			SuspendRebuild();

			var aabb = this.GetAxisAlignedBoundingBox();

			Children.Modify((list) =>
			{
				list.RemoveAll((i) => !(i is OperationSource));
			});

			// Fall back to sibling content if VertexSource is unset
			var vertexSource = this.VertexSource;

			// Convert VertexSource into expected Polygons
			Polygons polygonShape = (vertexSource == null) ? null : vertexSource.CreatePolygons();

			GenerateBase(polygonShape);

			if (aabb.ZSize > 0)
			{
				// If the part was already created and at a height, maintain the height.
				PlatingHelper.PlaceMeshAtHeight(this, aabb.minXYZ.Z);
			}
			ResumeRebuild();
<commit_msg>Fixed base (took out ObjectSource)<commit_after>﻿/*
Copyright (c) 2018, <NAME>, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using ClipperLib;
using MatterHackers.Agg.Transform;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters2D;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.VectorMath;
using Newtonsoft.Json;

namespace MatterHackers.MatterControl.DesignTools
{
	using Polygon = List<IntPoint>;
	using Polygons = List<List<IntPoint>>;
	public enum BaseTypes { None, Rectangle, Circle, /* Oval, Frame,*/ Outline, };

	public class BaseObject3D : Object3D, IPublicPropertyObject, IPropertyGridModifier
	{
		readonly double scalingForClipper = 1000;

		public BaseObject3D()
		{
			Name = "Base".Localize();
		}

		public override bool CanRemove => true;
		public override bool CanApply => true;

		public BaseTypes CurrentBaseType { get; set; }
		public double BaseSize { get; set; } = 3;
		public double InfillAmount { get; set; } = 3;
		public double ExtrusionHeight { get; set; } = 5;

		public override void Apply(UndoBuffer undoBuffer)
		{
			var visibleMeshes = this.VisibleMeshes().ToList();
			this.Children.Modify((list) =>
			{
				list.Clear();
				foreach(var child in visibleMeshes)
				{
					list.Add(new Object3D()
					{
						Mesh = child.Mesh
					});
				}
			});

			base.Apply(undoBuffer);
		}

		public override void Remove(UndoBuffer undoBuffer)
		{
			base.Remove(undoBuffer);
		}

		[JsonIgnore]
		public IVertexSource VertexSource
		{
			get
			{
				var vertexSource = (IPathObject)this.Descendants<IObject3D>().FirstOrDefault((i) => i is IPathObject);
				return vertexSource?.VertexSource;
			}
			set
			{
				var vertexSource = this.Children.OfType<IPathObject>().FirstOrDefault();
				if (vertexSource != null)
				{
					vertexSource.VertexSource = value;
				}
			}
		}

		public static BaseObject3D Create()
		{
			var item = new BaseObject3D();
			item.Rebuild(null);
			return item;
		}

		public override void OnInvalidate(InvalidateArgs invalidateType)
		{
			if ((invalidateType.InvalidateType == InvalidateType.Content
				|| invalidateType.InvalidateType == InvalidateType.Matrix
				|| invalidateType.InvalidateType == InvalidateType.Path
				|| invalidateType.InvalidateType == InvalidateType.Mesh)
				&& invalidateType.Source != this
				&& !RebuildSuspended)
			{
				Rebuild(null);
			}
			else
			{
				base.OnInvalidate(invalidateType);
			}
		}

		public override void Rebuild(UndoBuffer undoBuffer)
		{
			this.DebugDepth("Rebuild");

			SuspendRebuild();

			var aabb = this.GetAxisAlignedBoundingBox();

			Children.Modify((list) =>
			{
				if (list.Count > 0)
				{
					var first = list[0];
					list.Clear();
					list.Add(first);
				}
			});

			// Fall back to sibling content if VertexSource is unset
			var vertexSource = this.VertexSource;

			// Convert VertexSource into expected Polygons
			Polygons polygonShape = (vertexSource == null) ? null : vertexSource.CreatePolygons();

			GenerateBase(polygonShape);

			if (aabb.ZSize > 0)
			{
				// If the part was already created and at a height, maintain the height.
				PlatingHelper.PlaceMeshAtHeight(this, aabb.minXYZ.Z);
			}
			ResumeRebuild();

{'fraction_non_alphanumeric': 0.08239940228412851, 'fraction_numerical': 0.005016543921443057, 'mean_word_length': 4.14552443712246, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4876576', 'n_tokens_mistral': 3192, 'n_tokens_neox': 2649, 'n_words': 899}