Beispiel #1
0
        public async Task <IActionResult> CampaignReport()
        {
            CampaignReport campaignReport = new CampaignReport
            {
                //Get the locations, activities, and gatheringUsers from the database
                locations      = await _context.Location.ToListAsync(),
                activities     = await _context.Activity.Include(a => a.Location).Where(a => a.EndTime < DateTime.Now).ToListAsync(),
                gatheringUsers = await _context.GatheringUser.Include(gu => gu.Gathering).ThenInclude(g => g.Location).Where(gu => gu.Gathering.EndTime < DateTime.Now).ToListAsync()
            };

            //Go through each Activity and add the hours to the manhours property in the Location for the activity.
            campaignReport.activities.ForEach(a => campaignReport.locations.First(l => l.LocationId == a.LocationId).ManHours += (a.EndTime - a.StartTime).TotalHours);



            campaignReport.gatheringUsers.ForEach(gu => campaignReport.locations.FirstOrDefault(l => l.LocationId == gu.Gathering.LocationId).ManHours += (gu.Gathering.EndTime - gu.Gathering.StartTime).TotalHours);


            campaignReport.locations.ForEach(l => l.adjustedPopulation = l.Population - l.ManHours * 5);

            campaignReport.locations.ForEach(l => {
                l.adjustedPopulation = Math.Round(l.adjustedPopulation);
                l.ManHours           = Math.Round(l.ManHours, 2);
            });

            return(View(campaignReport));
        }
Beispiel #2
0
        public CampaignReport ValidateCampaign(CampaignVM campaignVM)
        {
            CampaignValidation validations = new CampaignValidation();
            var fileName = campaignVM.FileName;
            var fields   = GetAllMappingFields(campaignVM.Content);

            fields.AddRange(GetAllMappingFields(campaignVM.RecipientField));
            if (campaignVM.TypeId == (int)CampaignType.Email)
            {
                fields.AddRange(GetAllMappingFields(campaignVM.Subject));
            }

            var excelRows = GetExcelMappings(fileName);

            ValidateExcelForFields(ref validations, fields, excelRows);

            var validRows = new List <Row>();

            if (validations.IsValid)
            {
                int rowCount = 2;
                foreach (var row in excelRows)
                {
                    var recepient = row[GetAllMappingFields(campaignVM.RecipientField)[0]];
                    if (validations.Validate(recepient, (CampaignType)campaignVM.TypeId, rowCount))
                    {
                        validRows.Add(row);
                    }
                    rowCount += 1;
                }
            }
            validations.TotalRecordCount = excelRows.Count();
            var report = new CampaignReport
            {
                Campaign    = campaignVM,
                ExcelRows   = validRows,
                Validations = validations
            };

            return(report);
        }
Beispiel #3
0
        public int SaveCampaign(CampaignReport report)
        {
            try
            {
                var campaignVM = report.Campaign;

                CampaignService campaignService = new CampaignService();
                var             campaign        = new CampaignAdapter().GetCampaignFromVM(campaignVM);
                campaign.DepartmentId = UserModule.GetUsersDepartment();


                if (campaign.Id == 0)
                {
                    campaign.Id = campaignService.SaveCampaign(campaign);
                }
                else
                {
                    campaignService.ReCreateCampaign(campaign, GetCampaign(campaign.Id).TypeId);
                }


                if (campaignVM.TypeId == (int)CampaignType.Email)
                {
                    List <Email> emailList = new List <Email>();
                    foreach (var row in report.ExcelRows)
                    {
                        var   recipient = ReplaceContentWithValue(row, campaignVM.RecipientField);
                        Email email     = new Email
                        {
                            Body       = ReplaceContentWithValue(row, campaignVM.Content),
                            CampaignId = campaign.Id,
                            Id         = 0,
                            StatusId   = (int)MessageStatus.New,
                            StatusDate = DateTime.Now,
                            Subject    = ReplaceContentWithValue(row, campaignVM.Subject),
                            ToAddress  = recipient
                        };
                        emailList.Add(email);
                    }
                    campaignService.SaveCampaignEmails(emailList);
                }

                else
                {
                    List <Sms> smsList = new List <Sms>();
                    foreach (var row in report.ExcelRows)
                    {
                        var    recipient = ReplaceContentWithValue(row, campaignVM.RecipientField);
                        string number    = recipient.Replace("+", "").Replace("-", "").Replace(" ", "");;
                        Sms    sms       = new Sms
                        {
                            Content    = ReplaceContentWithValue(row, campaignVM.Content),
                            CampaignId = campaign.Id,
                            Id         = 0,
                            StatusId   = (int)MessageStatus.New,
                            StatusDate = DateTime.Now,
                            ToNumber   = number
                        };
                        smsList.Add(sms);
                    }



                    campaignService.SaveCampaignSmses(smsList);
                }
                return(campaign.Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<string> CreateSketch(Section section, Section symmetricSection, CampaignReport campaignReport, List<SteelThicknessGaugingPoint> steelThicknessGaugingPoints, out SortedList<int, List<SteelThicknessGaugingPoint>> pointsPerSketch)
        {
            try
            {
                if (section == null)
                {
                    throw new ArgumentNullException("section");
                }

                if (symmetricSection == null)
                {
                    throw new ArgumentNullException("symmetricSection");
                }

                if (campaignReport == null)
                {
                    throw new ArgumentNullException("campaignReport");
                }

                int index = 0;
                pointsPerSketch = new SortedList<int, List<SteelThicknessGaugingPoint>>();

                if (!section.SectionType.Name.Equals("Shell") && !section.SectionType.Name.Equals("Longitudinal") &&
                    !section.SectionType.Name.Equals("LongitudinalBulkheads") && !symmetricSection.SectionType.Name.Equals("Shell") &&
                    !symmetricSection.SectionType.Name.Equals("Longitudinal") && !symmetricSection.SectionType.Name.Equals("LongitudinalBulkheads")
                    )
                {
                    return null;
                }

                string sectionDrawerCurrentViewModeName = section.SectionDrawer.CurrentViewModeName;
                section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                Vessel vessel = section.Vessel;
                Document document = vessel.Document as Document;

                if (!this.sectionThumbnails.ContainsKey(vessel.GUID))
                {
                    this.sectionThumbnails.Add(vessel.GUID, new Dictionary<int, Bitmap>());
                }
                if (!this.sectionThumbnails[vessel.GUID].ContainsKey(section.ID))
                {
                    this.sectionThumbnails[vessel.GUID].Add(section.ID, null);
                }
                if (this.sectionThumbnails[vessel.GUID][section.ID] == null)
                {
                    CaptionsModel.Instance.SetCaptionPropertySelection("Plate", "IACSSketchDescription", false);

                    section.SectionDrawer.CurrentViewModeName = "Wireframe";

                    section.SectionDrawer.ConfigureView(this.glPanel, section.GetDefaultView(), true);
                    section.SectionDrawer.UpdateLastCamera(this.glPanel);

                    section.SectionDrawer.DrawAxis = false;
                    bool isWireframeVisible = ViewModesData.Instance.IsVisible(section.SectionDrawer.CurrentViewModeName, "Wireframe");
                    if (!isWireframeVisible)
                    {
                        ViewModesData.Instance.SetVisibility(section.SectionDrawer.CurrentViewModeName, "Wireframe", true, true);
                    }

                    Bitmap bitmap = section.SectionDrawer.DrawModelImmediateMode(this.glPanel);

                    if (!isWireframeVisible)
                    {
                        ViewModesData.Instance.SetVisibility(section.SectionDrawer.CurrentViewModeName, "Wireframe", false, true);
                    }

                    section.SectionDrawer.DrawAxis = true;

                    this.sectionThumbnails[vessel.GUID][section.ID] = bitmap;

                    section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                    CaptionsModel.Instance.SetCaptionPropertySelection("Plate", "IACSSketchDescription", true);
                }

                Bitmap originalThumbnail = this.sectionThumbnails[vessel.GUID][section.ID];

                List<string> bitmaps = new List<string>();

                List<SteelThicknessGaugingPoint> sectionSteelThicknessGaugingPoints = new List<SteelThicknessGaugingPoint>();
                List<SteelThicknessGaugingPoint> symmetricSteelThicknessGaugingPoints = new List<SteelThicknessGaugingPoint>();

                foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in steelThicknessGaugingPoints)
                {
                    if (steelThicknessGaugingPoint.Section.ID == section.ID)
                    {
                        sectionSteelThicknessGaugingPoints.Add(steelThicknessGaugingPoint);
                    }
                    else if (steelThicknessGaugingPoint.Section.ID == symmetricSection.ID)
                    {
                        symmetricSteelThicknessGaugingPoints.Add(steelThicknessGaugingPoint);
                    }
                }

                if (sectionSteelThicknessGaugingPoints != null || symmetricSteelThicknessGaugingPoints != null)
                {
                    #region Old Code

                    //int numberOfSketches = 9;

                    //Point3D minPoint = section.BoundingBox.MinPoint;
                    //Point3D maxPoint = section.BoundingBox.MaxPoint;

                    //double sizeX = maxPoint.X - minPoint.X;
                    //double deltaX = Math.Ceiling(sizeX / numberOfSketches);

                    //Vector3D planeNormal = new Vector3D(1, 0, 0);

                    //for (int i = 0; i < numberOfSketches; i++)
                    //{
                    //    Point3D minPlanePoint = new Point3D(minPoint.X + (i * deltaX), minPoint.Y, minPoint.Z);
                    //    Point3D maxPlanePoint = new Point3D(minPoint.X + ((i + 1) * deltaX), minPoint.Y, minPoint.Z);
                    //    Plane minPlane = new Plane(planeNormal, minPlanePoint);
                    //    Plane maxPlane = new Plane(planeNormal, maxPlanePoint);

                    #endregion

                    SortedList<float, SteelThicknessGaugingPoint> sortedSteelThicknessGaugingPoints = new SortedList<float, SteelThicknessGaugingPoint>();

                    foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in steelThicknessGaugingPoints)
                    {
                        float x = steelThicknessGaugingPoint.PointGeometry.Position.X;
                        while (sortedSteelThicknessGaugingPoints.ContainsKey(x))
                        {
                            x += (float)Point3D.Epsilon;
                        }

                        sortedSteelThicknessGaugingPoints.Add(x, steelThicknessGaugingPoint);
                    }

                    float minKey = sortedSteelThicknessGaugingPoints.Keys[0];
                    float maxKey = sortedSteelThicknessGaugingPoints.Keys[sortedSteelThicknessGaugingPoints.Count - 1];

                    Point3D minPoint = sortedSteelThicknessGaugingPoints[minKey].PointGeometry.Position;
                    Point3D maxPoint = sortedSteelThicknessGaugingPoints[maxKey].PointGeometry.Position;

                    minPoint.Translate(-2 * Point3D.Epsilon, 0, 0);
                    maxPoint.Translate(2 * Point3D.Epsilon, 0, 0);

                    double defaultSketchSize = 40;
                    double sizeX = maxPoint.X - minPoint.X;

                    int numberOfSketches = (int)Math.Floor(sizeX / defaultSketchSize);

                    if (sizeX < defaultSketchSize)
                    {
                        defaultSketchSize = sizeX;
                        numberOfSketches = 1;
                    }
                    else
                    {
                        double mod = sizeX - (numberOfSketches * defaultSketchSize);
                        if (mod > (defaultSketchSize / 2))
                        {
                            numberOfSketches++;
                            defaultSketchSize -= (mod / numberOfSketches);
                        }
                        else
                        {
                            defaultSketchSize += (mod / numberOfSketches);
                        }
                    }

                    Vector3D planeNormal = new Vector3D(1, 0, 0);
                    for (int i = 0; i < numberOfSketches; i++)
                    {
                        Point3D minPlanePoint = new Point3D(minPoint.X + (i * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Point3D maxPlanePoint = new Point3D(minPoint.X + ((i + 1) * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Plane minPlane = new Plane(planeNormal, minPlanePoint);
                        Plane maxPlane = new Plane(planeNormal, maxPlanePoint);

                        List<SteelThicknessGaugingPoint> sectionGaugingPointsBySketch = (sectionSteelThicknessGaugingPoints != null) ? this.GetPointsBetweenPlanes(sectionSteelThicknessGaugingPoints, minPlane, maxPlane) : null;
                        List<SteelThicknessGaugingPoint> symmetricGaugingPointsBySketch = (symmetricSteelThicknessGaugingPoints != null) ? this.GetPointsBetweenPlanes(symmetricSteelThicknessGaugingPoints, minPlane, maxPlane) : null;

                        if ((sectionGaugingPointsBySketch == null || sectionGaugingPointsBySketch.Count == 0) &&
                            (symmetricGaugingPointsBySketch == null || symmetricGaugingPointsBySketch.Count == 0))
                        {
                            continue;
                        }

                        List<SteelThicknessGaugingPoint> gaugingPointsBySketch = new List<SteelThicknessGaugingPoint>();
                        if (sectionGaugingPointsBySketch != null && sectionGaugingPointsBySketch.Count > 0)
                        {
                            gaugingPointsBySketch.AddRange(sectionGaugingPointsBySketch);
                        }
                        if (symmetricGaugingPointsBySketch != null && symmetricGaugingPointsBySketch.Count > 0)
                        {
                            gaugingPointsBySketch.AddRange(symmetricGaugingPointsBySketch);
                        }

                        pointsPerSketch.Add(index, gaugingPointsBySketch);
                        index++;

                        #region Compute gauging points in symmetric section

                        List<SteelThicknessGaugingPoint> pointsToBeDeleted = new List<SteelThicknessGaugingPoint>();

                        if (symmetricGaugingPointsBySketch != null && symmetricGaugingPointsBySketch.Count > 0)
                        {
                            #region Clone gauging points only in SB side, to PS side to be renderized in sketch

                            #region Find center plane parallel to X and Z axis

                            BoundingBox sectionBoundingBox = new BoundingBox();
                            BoundingBox symmetricBoundingBox = new BoundingBox();

                            List<Campaign> originalCampaigns = vessel.GetOriginalCampaigns();
                            foreach (Campaign campaign in originalCampaigns)
                            {
                                IList platesInCampaign = section.GetPermanentElementsInCampaign(campaign, true, "Plate");
                                if (platesInCampaign != null && platesInCampaign.Count > 0)
                                {
                                    foreach (Plate plate in platesInCampaign)
                                    {
                                        sectionBoundingBox.Union(plate.Design.Geometry.BoundingBox);
                                    }
                                }

                                platesInCampaign = symmetricSection.GetPermanentElementsInCampaign(campaign, true, "Plate");
                                if (platesInCampaign != null && platesInCampaign.Count > 0)
                                {
                                    foreach (Plate plate in platesInCampaign)
                                    {
                                        symmetricBoundingBox.Union(plate.Design.Geometry.BoundingBox);
                                    }
                                }
                            }

                            BoundingBox boundingBox = new BoundingBox(sectionBoundingBox);
                            boundingBox.Union(symmetricBoundingBox);

                            Plane plane = new Plane(new Vector3D(0, 1, 0), boundingBox.Center);

                            #endregion

                            SortedList<int, List<SteelThicknessGaugingPoint>> sectionOrganizedGaugingPoints = this.OrganizeIACSGaugingPointsByID(sectionGaugingPointsBySketch);
                            foreach (SteelThicknessGaugingPoint symmetricGaugingPoint in symmetricGaugingPointsBySketch)
                            {
                                if (sectionOrganizedGaugingPoints.ContainsKey((int)symmetricGaugingPoint.IACSReportLocationData.ID))
                                {
                                    bool haveSymmetricPoint = false;
                                    foreach (SteelThicknessGaugingPoint symmetricOrganizedGaugingPoint in sectionOrganizedGaugingPoints[(int)symmetricGaugingPoint.IACSReportLocationData.ID])
                                    {
                                        if (symmetricOrganizedGaugingPoint.IACSReportLocationData.IsForward == symmetricGaugingPoint.IACSReportLocationData.IsForward)
                                        {
                                            haveSymmetricPoint = true;
                                            break;
                                        }
                                    }

                                    if (haveSymmetricPoint)
                                    {
                                        continue;
                                    }
                                }

                                Point3D pointPosition = new Point3D(symmetricGaugingPoint.PointGeometry.Position);
                                pointPosition = (Point3D)pointPosition.Mirror(plane);
                                Vector3D pointNormal = new Vector3D(symmetricGaugingPoint.PointGeometry.Normal * -1);

                                int geometryID, geometryPartID;
                                List<string> elementTypeNames = new List<string>();
                                elementTypeNames.Add(symmetricGaugingPoint.TargetElement.GetBaseType().Name);
                                Point3D closestPoint;
                                SectionElement sectionElement = section.PointOverDomainEntity(pointPosition, elementTypeNames, 0.01, out closestPoint, out geometryID, out geometryPartID) as SectionElement;
                                if (sectionElement == null)
                                {
                                    continue;
                                }

                                AverageSteelThicknessGaugingPoint averageSteelThicknessGaugingPoint = new AverageSteelThicknessGaugingPoint();

                                averageSteelThicknessGaugingPoint.PointGeometry.Position = pointPosition;
                                averageSteelThicknessGaugingPoint.PointGeometry.Normal = pointNormal;

                                averageSteelThicknessGaugingPoint.Section = section;
                                averageSteelThicknessGaugingPoint.Campaign = campaignReport.Campaign;
                                averageSteelThicknessGaugingPoint.CampaignReport = campaignReport;
                                averageSteelThicknessGaugingPoint.TargetElement = sectionElement as IWorkableElement;
                                averageSteelThicknessGaugingPoint.TargetElementGeometryID = geometryID;
                                averageSteelThicknessGaugingPoint.TargetElementGeometrySegmentID = geometryPartID;
                                averageSteelThicknessGaugingPoint.TargetElementPart = symmetricGaugingPoint.TargetElementPart;
                                averageSteelThicknessGaugingPoint.Active = true;

                                averageSteelThicknessGaugingPoint.IACSReportLocationData.ID = symmetricGaugingPoint.IACSReportLocationData.ID;
                                averageSteelThicknessGaugingPoint.IACSReportLocationData.SetColumnNull();
                                averageSteelThicknessGaugingPoint.IACSReportLocationData.Strake = symmetricGaugingPoint.IACSReportLocationData.Strake;
                                averageSteelThicknessGaugingPoint.IACSReportLocationData.IsForward = (bool)symmetricGaugingPoint.IACSReportLocationData.IsForward;
                                averageSteelThicknessGaugingPoint.IACSReportLocationData.IsPortSide = !(bool)symmetricGaugingPoint.IACSReportLocationData.IsPortSide;

                                //#if (DEBUG)
                                //newFrontGaugingPoint.Name = ((bool)newFrontGaugingPoint.IACSReportLocationData.IsForward) ? "F " : "B ";
                                //newFrontGaugingPoint.Name += newFrontGaugingPoint.IACSReportLocationData.ID.ToString();
                                //newFrontGaugingPoint.Name += ((bool)newFrontGaugingPoint.IACSReportLocationData.IsPortSide) ? " P " : " S ";
                                //#else
                                averageSteelThicknessGaugingPoint.Name = averageSteelThicknessGaugingPoint.IACSReportLocationData.ID.ToString();
                                //#endif
                                section.AddPermanentDomainEntityToGroup(averageSteelThicknessGaugingPoint, true, false);
                                //section.Vessel.Document.DocumentStateController.BroadcastEntityCreation(this, averageSteelThicknessGaugingPoint);

                                sectionGaugingPointsBySketch.Add(averageSteelThicknessGaugingPoint);
                                pointsToBeDeleted.Add(averageSteelThicknessGaugingPoint);
                            }

                            #endregion
                        }

                        #endregion

                        #region Remove computed gauging points of collections to avoid unnecessary computations

                        int sketchPointsCount = 0;
                        Vector3D sketchNormal = new Vector3D();

                        if (sectionGaugingPointsBySketch != null)
                        {
                            foreach (SteelThicknessGaugingPoint sectionGaugingPointBySketch in sectionGaugingPointsBySketch)
                            {
                                sketchNormal += sectionGaugingPointBySketch.PointGeometry.Normal;

                                if (sectionSteelThicknessGaugingPoints.Contains(sectionGaugingPointBySketch))
                                {
                                    sectionSteelThicknessGaugingPoints.Remove(sectionGaugingPointBySketch);
                                }
                            }

                            sketchPointsCount += sectionGaugingPointsBySketch.Count;
                        }

                        if (symmetricGaugingPointsBySketch != null)
                        {
                            foreach (SteelThicknessGaugingPoint symmetricGaugingPointBySketch in symmetricGaugingPointsBySketch)
                            {
                                sketchNormal += symmetricGaugingPointBySketch.PointGeometry.Normal;

                                if (symmetricSteelThicknessGaugingPoints.Contains(symmetricGaugingPointBySketch))
                                {
                                    symmetricSteelThicknessGaugingPoints.Remove(symmetricGaugingPointBySketch);
                                }
                            }

                            sketchPointsCount += symmetricGaugingPointsBySketch.Count;
                        }

                        sketchNormal /= sketchPointsCount;
                        sketchNormal.Normalize();

                        #endregion

                        if (sectionGaugingPointsBySketch.Count > 0)
                        {
                            List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, minPlane, maxPlane, sectionGaugingPointsBySketch, sketchNormal);

                            // Generate thumbnail
                            section.SectionDrawer.ConfigureView(this.glPanel, section.GetDefaultView(), true);
                            section.SectionDrawer.UpdateLastCamera(this.glPanel);

                            Bitmap thumbnail = new Bitmap(originalThumbnail, originalThumbnail.Width, originalThumbnail.Height);

                            Rectangle rectangle = this.GetRectangleOfRenderizedArea(sectionElements);
                            for (int j = 0; j < thumbnail.Width; j++)
                            {
                                for (int k = 0; k < thumbnail.Height; k++)
                                {
                                    if (this.IsPixelOfRectangle(j, k, rectangle, 3))
                                    {
                                        thumbnail.SetPixel(j, k, Color.Red);
                                    }
                                }
                            }

                            string thumbName = thumbnail.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                            this.bitmapCount++;

                            thumbName = document.HDF5DataAccessController.SaveBitmap(thumbnail, thumbName);
                            bitmaps.Add(thumbName);

                            C4DView view = section.SectionDrawer.ComputeViewForSectionElements(this.glPanel, sectionElements, false, 1.2f, true);
                            if (view == null)
                            {
                                view = section.GetDefaultView();
                            }

                            section.SectionDrawer.ConfigureView(this.glPanel, view, false);
                            section.SectionDrawer.UpdateLastCamera(this.glPanel);

                            List<int> gaugingPointIDs = new List<int>();
                            foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in sectionGaugingPointsBySketch)
                            {
                                gaugingPointIDs.Add(steelThicknessGaugingPoint.ID);
                            }

                            section.SectionDrawer.SetPriorityVisibleElements("SteelThicknessGaugingPoint", gaugingPointIDs);

                            Bitmap bitmap = section.SectionDrawer.DrawModelImmediateModeCuttingOfCompartment(this.glPanel);

                            section.SectionDrawer.ClearPriorityVisibleElements("SteelThicknessGaugingPoint");

                            string bitmapName = bitmap.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                            this.bitmapCount++;

                            bitmapName = document.HDF5DataAccessController.SaveBitmap(bitmap, bitmapName);
                            bitmaps.Add(bitmapName);

                            bitmap.Dispose();
                        }

                        foreach (SteelThicknessGaugingPoint pointToBeDeleted in pointsToBeDeleted)
                        {
                            document.DeleteSectionElement(pointToBeDeleted, false);
                        }
                    }
                }

                section.SectionDrawer.CurrentViewModeName = sectionDrawerCurrentViewModeName;

                return bitmaps;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Sketch for section/campaign report cannot be created!", ex);
            }
        }
        private void ToAverageInOtherTypesSection(Section section, CampaignReport sourceCampaignReport, ref int initialIacsID)
        {
            try
            {
                Dictionary<string, Dictionary<ISectionElement, List<IGaugingElement>>> gaugingPointsByStructureElementType = section.GetGaugingElementsBySectionElementInCampaignReport("SteelThicknessGaugingPoint", sourceCampaignReport);
                if (gaugingPointsByStructureElementType == null || gaugingPointsByStructureElementType.Count == 0)
                    return;

                List<SteelThicknessGaugingPoint> averageGaugingPoints = null;
                this.averageGaugingsCreator.CreateAverageGaugingPointsInNonLongitudinalSection(section, gaugingPointsByStructureElementType, true, out averageGaugingPoints);

                this.AddAverageGaugingPointsInSection(section, averageGaugingPoints);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Cannot create average gauging points in Other Types Section!", ex);
            }
        }
        public List<string> CreateSketch(Section section, CampaignReport campaignReport, List<SteelThicknessGaugingPoint> steelThicknessGaugingPoints, out SortedList<int, List<SteelThicknessGaugingPoint>> pointsPerSketch)
        {
            try
            {
                if (section == null)
                {
                    throw new ArgumentNullException("section");
                }

                if (campaignReport == null)
                {
                    throw new ArgumentNullException("campaignReport");
                }

                int index = 0;
                pointsPerSketch = new SortedList<int, List<SteelThicknessGaugingPoint>>();

                SortedList<string, IList> elementsInCampaign = section.GetPermanentElementsInCampaignReport(campaignReport);
                if (!elementsInCampaign.ContainsKey("SteelThicknessGaugingPoint") || elementsInCampaign["SteelThicknessGaugingPoint"].Count == 0)
                {
                    return null;
                }

                Document document = campaignReport.Vessel.Document as Document;
                Vessel vessel = document.Vessel;

                List<string> bitmaps = new List<string>();

                string sectionDrawerCurrentViewModeName = section.SectionDrawer.CurrentViewModeName;
                section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                if (!this.sectionThumbnails.ContainsKey(vessel.GUID))
                {
                    this.sectionThumbnails.Add(vessel.GUID, new Dictionary<int, Bitmap>());
                }
                if (!this.sectionThumbnails[vessel.GUID].ContainsKey(section.ID))
                {
                    this.sectionThumbnails[vessel.GUID].Add(section.ID, null);
                }
                if (this.sectionThumbnails[vessel.GUID][section.ID] == null)
                {
                    CaptionsModel.Instance.SetCaptionPropertySelection("Plate", "IACSSketchDescription", false);

                    section.SectionDrawer.CurrentViewModeName = "Wireframe";

                    section.SectionDrawer.ConfigureView(this.glPanel, section.GetDefaultView(), true);
                    section.SectionDrawer.UpdateLastCamera(this.glPanel);

                    section.SectionDrawer.DrawAxis = false;
                    bool isWireframeVisible = ViewModesData.Instance.IsVisible(section.SectionDrawer.CurrentViewModeName, "Wireframe");
                    if (!isWireframeVisible)
                    {
                        ViewModesData.Instance.SetVisibility(section.SectionDrawer.CurrentViewModeName, "Wireframe", true, true);
                    }

                    Bitmap bitmap = section.SectionDrawer.DrawModelImmediateMode(this.glPanel);

                    if (!isWireframeVisible)
                    {
                        ViewModesData.Instance.SetVisibility(section.SectionDrawer.CurrentViewModeName, "Wireframe", false, true);
                    }
                    section.SectionDrawer.DrawAxis = true;

                    this.sectionThumbnails[vessel.GUID][section.ID] = bitmap;

                    section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                    CaptionsModel.Instance.SetCaptionPropertySelection("Plate", "IACSSketchDescription", true);
                }

                Bitmap originalThumbnail = this.sectionThumbnails[vessel.GUID][section.ID];

                if (section.SectionType.Name.Equals("WebFrames") ||
                    section.SectionType.Name.Equals("TransverseBulkheads") ||
                    section.SectionType.Name.Equals("TransverseCentralBulkheads") ||
                    section.SectionType.Name.Equals("SwashBulkheads")
                   )
                {
                    #region Non Longitudinal

                    pointsPerSketch.Add(index, steelThicknessGaugingPoints);
                    index++;

                    BoundingBox boundingBox = section.GetBoundingBoxOfElementType("Plate");

                    Vector3D planeNormal = section.Normal * section.Up;
                    Point3D planePoint = boundingBox.Center;
                    Plane plane = new Plane(planeNormal, planePoint);

                    List<SteelThicknessGaugingPoint> backGaugingPoints = null;
                    List<SteelThicknessGaugingPoint> frontGaugingPoints = null;

                    this.averageGaugingsCreator.SeparateGaugingPoints(steelThicknessGaugingPoints, plane, out backGaugingPoints, out frontGaugingPoints);

                    SortedList<int, List<SteelThicknessGaugingPoint>> backOrganizedGaugingPoints = this.OrganizeIACSGaugingPointsByID(backGaugingPoints);
                    SortedList<int, List<SteelThicknessGaugingPoint>> frontOrganizedGaugingPoints = this.OrganizeIACSGaugingPointsByID(frontGaugingPoints);

                    #region Clone gauging points that only exists in source side, and copy to target side

                    List<SteelThicknessGaugingPoint> pointsToBeDeleted = new List<SteelThicknessGaugingPoint>();

                    List<SteelThicknessGaugingPoint> sourceGaugingPoints = (backGaugingPoints.Count < frontGaugingPoints.Count) ? backGaugingPoints : frontGaugingPoints;
                    List<SteelThicknessGaugingPoint> targetGaugingPoints = (backGaugingPoints.Count < frontGaugingPoints.Count) ? frontGaugingPoints : backGaugingPoints;

                    List<int> sourceGaugingPointsIACSID = (backGaugingPoints.Count < frontGaugingPoints.Count) ? new List<int>(backOrganizedGaugingPoints.Keys) : new List<int>(frontOrganizedGaugingPoints.Keys);
                    List<int> targetGaugingPointsIACSID = (backGaugingPoints.Count < frontGaugingPoints.Count) ? new List<int>(frontOrganizedGaugingPoints.Keys) : new List<int>(backOrganizedGaugingPoints.Keys);

                    foreach (SteelThicknessGaugingPoint sourceGaugingPoint in sourceGaugingPoints)
                    {
                        if (targetGaugingPointsIACSID.Contains((int)sourceGaugingPoint.IACSReportLocationData.ID))
                        {
                            continue;
                        }

                        Point3D pointPosition = new Point3D(sourceGaugingPoint.PointGeometry.Position);
                        pointPosition = (Point3D)pointPosition.Mirror(plane);
                        Vector3D pointNormal = new Vector3D(sourceGaugingPoint.PointGeometry.Normal);

                        int geometryID, geometryPartID;
                        List<string> elementTypeNames = new List<string>();
                        elementTypeNames.Add(sourceGaugingPoint.TargetElement.GetBaseType().Name);
                        Point3D closestPoint;
                        SectionElement sectionElement = section.PointOverDomainEntity(pointPosition, elementTypeNames, 0.01, out closestPoint, out geometryID, out geometryPartID) as SectionElement;
                        if (sectionElement == null)
                        {
                            continue;
                        }

                        AverageSteelThicknessGaugingPoint averageSteelThicknessGaugingPoint = new AverageSteelThicknessGaugingPoint();

                        averageSteelThicknessGaugingPoint.PointGeometry.Position = pointPosition;
                        averageSteelThicknessGaugingPoint.PointGeometry.Normal = pointNormal;

                        averageSteelThicknessGaugingPoint.Section = section;
                        averageSteelThicknessGaugingPoint.Campaign = campaignReport.Campaign;
                        averageSteelThicknessGaugingPoint.CampaignReport = campaignReport;
                        averageSteelThicknessGaugingPoint.TargetElement = sectionElement as IWorkableElement;
                        averageSteelThicknessGaugingPoint.TargetElementGeometryID = geometryID;
                        averageSteelThicknessGaugingPoint.TargetElementGeometrySegmentID = geometryPartID;
                        averageSteelThicknessGaugingPoint.TargetElementPart = sourceGaugingPoint.TargetElementPart;
                        averageSteelThicknessGaugingPoint.Active = true;
                        averageSteelThicknessGaugingPoint.GaugingValue = sourceGaugingPoint.GaugingValue;

                        averageSteelThicknessGaugingPoint.IACSReportLocationData.ID = sourceGaugingPoint.IACSReportLocationData.ID;
                        averageSteelThicknessGaugingPoint.IACSReportLocationData.SetColumnNull();
                        averageSteelThicknessGaugingPoint.IACSReportLocationData.Strake = sourceGaugingPoint.IACSReportLocationData.Strake;
                        averageSteelThicknessGaugingPoint.IACSReportLocationData.IsPortSide = !(bool)sourceGaugingPoint.IACSReportLocationData.IsPortSide;

                        averageSteelThicknessGaugingPoint.Name = averageSteelThicknessGaugingPoint.IACSReportLocationData.ID.ToString();

                        //#if (DEBUG)
                        //                        newFrontGaugingPoint.Name += ((bool)newFrontGaugingPoint.IACSReportLocationData.IsPortSide) ? " P " : " S ";
                        //#endif

                        section.AddPermanentDomainEntityToGroup(averageSteelThicknessGaugingPoint, true, false);
                        //section.Vessel.Document.DocumentStateController.BroadcastEntityCreation(this, averageSteelThicknessGaugingPoint);

                        targetGaugingPoints.Add(averageSteelThicknessGaugingPoint);
                        pointsToBeDeleted.Add(averageSteelThicknessGaugingPoint);
                    }

                    #endregion

                    List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, targetGaugingPoints);

                    // Generate thumbnail
                    Bitmap thumbnail = originalThumbnail.Clone() as Bitmap;

                    Rectangle rectangle = this.GetRectangleOfRenderizedArea(sectionElements);
                    for (int i = 0; i < thumbnail.Width; i++)
                    {
                        for (int j = 0; j < thumbnail.Height; j++)
                        {
                            if (this.IsPixelOfRectangle(i, j, rectangle, 3))
                            {
                                thumbnail.SetPixel(i, j, Color.Red);
                            }
                        }
                    }

                    //int newWidht = (int)(thumbnail.Width / 5);
                    //int newHeight = (int)(thumbnail.Height / 5);
                    //Image image = thumbnail.GetThumbnailImage(newWidht, newHeight, null, IntPtr.Zero);
                    //thumbnail = new Bitmap(image);

                    string thumbName = thumbnail.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                    this.bitmapCount++;

                    thumbName = document.HDF5DataAccessController.SaveBitmap(thumbnail, thumbName);
                    bitmaps.Add(thumbName);

                    //section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                    C4DView view = section.SectionDrawer.ComputeViewForSectionElements(this.glPanel, sectionElements, false, 1.2f, true);
                    if (view == null)
                    {
                        view = section.GetDefaultView();
                    }

                    section.SectionDrawer.ConfigureView(this.glPanel, view, false);
                    section.SectionDrawer.UpdateLastCamera(this.glPanel);

                    // Get IDs of gauging points to set in SectionDrawer
                    List<int> pointIDs = new List<int>();
                    foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in targetGaugingPoints)
                    {
                        pointIDs.Add(steelThicknessGaugingPoint.ID);
                    }

                    section.SectionDrawer.SetPriorityVisibleElements("SteelThicknessGaugingPoint", pointIDs);

                    Bitmap bitmap = section.SectionDrawer.DrawModelImmediateModeCuttingOfCompartment(this.glPanel);

                    section.SectionDrawer.ClearPriorityVisibleElements("SteelThicknessGaugingPoint");

                    string bitmapName = bitmap.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                    this.bitmapCount++;

                    bitmapName = document.HDF5DataAccessController.SaveBitmap(bitmap, bitmapName);
                    bitmaps.Add(bitmapName);

                    bitmap.Dispose();

                    foreach (SteelThicknessGaugingPoint pointToBeDeleted in pointsToBeDeleted)
                    {
                        document.DeleteSectionElement(pointToBeDeleted, false);
                    }

                    #endregion
                }
                else if (section.SectionType.Name.Equals("MainDeck") ||
                  section.SectionType.Name.Equals("OtherDecks") ||
                  section.SectionType.Name.Equals("InnerBottom") ||
                  section.SectionType.Name.Equals("Bottom")
                 )
                {
                    #region Decks and Inner Bottom

                    #region Old Code

                    //// Get plates in campaigns of construction or in campaigns of conversion
                    //SortedList<int, Plate> originalPlates = new SortedList<int, Plate>();

                    //List<Campaign> originalCampaigns = vessel.GetOriginalCampaigns();
                    //foreach (Campaign campaign in originalCampaigns)
                    //{
                    //    SortedList<string, IList> platesInCampaign = section.GetElementsInCampaign(campaign, true, "Plate");
                    //    if (platesInCampaign.ContainsKey("Plate"))
                    //    {
                    //        List<Plate> platesInCampaignList = platesInCampaign["Plate"].Cast<Plate>().ToList();
                    //        foreach (Plate plate in platesInCampaignList)
                    //        {
                    //            originalPlates.Add(plate.ID, plate);
                    //        }
                    //    }
                    //}

                    //// Get plates in center column
                    //Plane centerPlaneYZ = new Plane(new Vector3D(1, 0, 0), section.BoundingBox.Center);
                    //SortedList<int, Plate> intersectedPlatesYZ = this.IntersectPlates(originalPlates, centerPlaneYZ);

                    //// Get bounding box of plates in center column
                    //BoundingBox boundingBox = new BoundingBox();
                    //foreach (Plate plate in intersectedPlatesYZ.Values)
                    //{
                    //    boundingBox.Union(plate.Design.Geometry.BoundingBox);
                    //}

                    //Plane centerPlaneXZ = new Plane(new Vector3D(0, 1, 0), boundingBox.Center);

                    //Vector3D xAxis = new Vector3D(1, 0, 0);
                    //SortedList<double, Compartment> sortedCompartments = this.IntersectCompartments(section, centerPlaneXZ);
                    //foreach (Compartment compartment in sortedCompartments.Values)
                    //{
                    //    Plane minPlane = new Plane(xAxis, compartment.BoundingBox.MinPoint);
                    //    Plane maxPlane = new Plane(xAxis, compartment.BoundingBox.MaxPoint);
                    //    List<SteelThicknessGaugingPoint> pointsBetweenPlanes = this.GetPointsBetweenPlanes(steelThicknessGaugingPoints, minPlane, maxPlane);

                    #endregion

                    SortedList<float, SteelThicknessGaugingPoint> sortedSteelThicknessGaugingPoints = new SortedList<float, SteelThicknessGaugingPoint>();

                    foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in steelThicknessGaugingPoints)
                    {
                        float x = steelThicknessGaugingPoint.PointGeometry.Position.X;
                        while (sortedSteelThicknessGaugingPoints.ContainsKey(x))
                        {
                            x += (float)Point3D.Epsilon;
                        }

                        sortedSteelThicknessGaugingPoints.Add(x, steelThicknessGaugingPoint);
                    }

                    float minKey = sortedSteelThicknessGaugingPoints.Keys[0];
                    float maxKey = sortedSteelThicknessGaugingPoints.Keys[sortedSteelThicknessGaugingPoints.Count - 1];

                    Point3D minPoint = sortedSteelThicknessGaugingPoints[minKey].PointGeometry.Position;
                    Point3D maxPoint = sortedSteelThicknessGaugingPoints[maxKey].PointGeometry.Position;

                    minPoint.Translate(-2 * Point3D.Epsilon, 0, 0);
                    maxPoint.Translate(2 * Point3D.Epsilon, 0, 0);

                    double defaultSketchSize = 40;
                    double sizeX = maxPoint.X - minPoint.X;

                    int numberOfSketches = (int)Math.Floor(sizeX / defaultSketchSize);

                    if (sizeX < defaultSketchSize)
                    {
                        defaultSketchSize = sizeX;
                        numberOfSketches = 1;
                    }
                    else
                    {
                        double mod = sizeX - (numberOfSketches * defaultSketchSize);
                        if (mod > (defaultSketchSize / 2))
                        {
                            numberOfSketches++;
                            defaultSketchSize -= (mod / numberOfSketches);
                        }
                        else
                        {
                            defaultSketchSize += (mod / numberOfSketches);
                        }
                    }

                    Vector3D planeNormal = new Vector3D(1, 0, 0);
                    for (int i = 0; i < numberOfSketches; i++)
                    {
                        Point3D minPlanePoint = new Point3D(minPoint.X + (i * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Point3D maxPlanePoint = new Point3D(minPoint.X + ((i + 1) * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Plane minPlane = new Plane(planeNormal, minPlanePoint);
                        Plane maxPlane = new Plane(planeNormal, maxPlanePoint);
                        List<SteelThicknessGaugingPoint> pointsBetweenPlanes = this.GetPointsBetweenPlanes(steelThicknessGaugingPoints, minPlane, maxPlane);

                        if (pointsBetweenPlanes == null || pointsBetweenPlanes.Count == 0)
                        {
                            continue;
                        }

                        pointsPerSketch.Add(index, pointsBetweenPlanes);
                        index++;

                        foreach (SteelThicknessGaugingPoint pointBetweenPlane in pointsBetweenPlanes)
                        {
                            if (steelThicknessGaugingPoints.Contains(pointBetweenPlane))
                            {
                                steelThicknessGaugingPoints.Remove(pointBetweenPlane);
                            }
                        }

                        //List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, compartment);
                        List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, minPlane, maxPlane);

                        // Generate thumbnail
                        section.SectionDrawer.ConfigureView(this.glPanel, section.GetDefaultView(), true);
                        section.SectionDrawer.UpdateLastCamera(this.glPanel);

                        Bitmap thumbnail = originalThumbnail.Clone() as Bitmap;

                        Rectangle rectangle = this.GetRectangleOfRenderizedArea(sectionElements);
                        for (int j = 0; j < thumbnail.Width; j++)
                        {
                            for (int k = 0; k < thumbnail.Height; k++)
                            {
                                if (this.IsPixelOfRectangle(j, k, rectangle, 3))
                                {
                                    thumbnail.SetPixel(j, k, Color.Red);
                                }
                            }
                        }

                        //int newWidht = (int)(thumbnail.Width / 5);
                        //int newHeight = (int)(thumbnail.Height / 5);
                        //Image image = thumbnail.GetThumbnailImage(newWidht, newHeight, null, IntPtr.Zero);
                        //thumbnail = new Bitmap(image);

                        string thumbName = thumbnail.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                        this.bitmapCount++;

                        thumbName = document.HDF5DataAccessController.SaveBitmap(thumbnail, thumbName);
                        bitmaps.Add(thumbName);

                        C4DView view = section.SectionDrawer.ComputeViewForSectionElements(this.glPanel, sectionElements, false, 1.2f, true);
                        if (view == null)
                        {
                            view = section.GetDefaultView();
                        }

                        section.SectionDrawer.ConfigureView(this.glPanel, view, false);
                        section.SectionDrawer.UpdateLastCamera(this.glPanel);

                        List<int> pointIDs = new List<int>();
                        foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in pointsBetweenPlanes)
                        {
                            pointIDs.Add(steelThicknessGaugingPoint.ID);
                        }

                        section.SectionDrawer.SetPriorityVisibleElements("SteelThicknessGaugingPoint", pointIDs);

                        Bitmap bitmap = section.SectionDrawer.DrawModelImmediateModeCuttingOfCompartment(this.glPanel);

                        section.SectionDrawer.ClearPriorityVisibleElements("SteelThicknessGaugingPoint");

                        string bitmapName = bitmap.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                        this.bitmapCount++;

                        bitmapName = document.HDF5DataAccessController.SaveBitmap(bitmap, bitmapName);
                        bitmaps.Add(bitmapName);

                        bitmap.Dispose();
                    }

                    #endregion
                }
                else if (section.SectionType.Name.Equals("Shell") ||
                         section.SectionType.Name.Equals("Longitudinal") ||
                         section.SectionType.Name.Equals("LongitudinalBulkheads") ||
                         section.SectionType.Name.Equals("LongitudinalCentralBulkheads")
                 )
                {
                    #region Symmetric Longitudinal

                    #region Old Code

                    //int numberOfSketches = 9;

                    //BoundingBox sectionBoundingBox = section.GetBoundingBoxOfElementType("Plate");

                    //Point3D minPoint = sectionBoundingBox.MinPoint;
                    //Point3D maxPoint = sectionBoundingBox.MaxPoint;

                    //double sizeX = maxPoint.X - minPoint.X;
                    //double deltaX = Math.Ceiling(sizeX / numberOfSketches);

                    //Vector3D planeNormal = new Vector3D(1, 0, 0);
                    //for (int i = 0; i < numberOfSketches; i++)
                    //{
                    //    Point3D minPlanePoint = new Point3D(minPoint.X + (i * deltaX), minPoint.Y, minPoint.Z);
                    //    Point3D maxPlanePoint = new Point3D(minPoint.X + ((i + 1) * deltaX), minPoint.Y, minPoint.Z);
                    //    Plane minPlane = new Plane(planeNormal, minPlanePoint);
                    //    Plane maxPlane = new Plane(planeNormal, maxPlanePoint);

                    #endregion

                    SortedList<float, SteelThicknessGaugingPoint> sortedSteelThicknessGaugingPoints = new SortedList<float, SteelThicknessGaugingPoint>();

                    foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in steelThicknessGaugingPoints)
                    {
                        float x = steelThicknessGaugingPoint.PointGeometry.Position.X;
                        while (sortedSteelThicknessGaugingPoints.ContainsKey(x))
                        {
                            x += (float)Point3D.Epsilon;
                        }

                        sortedSteelThicknessGaugingPoints.Add(x, steelThicknessGaugingPoint);
                    }

                    float minKey = sortedSteelThicknessGaugingPoints.Keys[0];
                    float maxKey = sortedSteelThicknessGaugingPoints.Keys[sortedSteelThicknessGaugingPoints.Count - 1];

                    Point3D minPoint = sortedSteelThicknessGaugingPoints[minKey].PointGeometry.Position;
                    Point3D maxPoint = sortedSteelThicknessGaugingPoints[maxKey].PointGeometry.Position;

                    minPoint.Translate(-2 * Point3D.Epsilon, 0, 0);
                    maxPoint.Translate(2 * Point3D.Epsilon, 0, 0);

                    double defaultSketchSize = 40;
                    double sizeX = maxPoint.X - minPoint.X;

                    int numberOfSketches = (int)Math.Floor(sizeX / defaultSketchSize);

                    if (sizeX < defaultSketchSize)
                    {
                        defaultSketchSize = sizeX;
                        numberOfSketches = 1;
                    }
                    else
                    {
                        double mod = sizeX - (numberOfSketches * defaultSketchSize);
                        if (mod > (defaultSketchSize / 2))
                        {
                            numberOfSketches++;
                            defaultSketchSize -= (mod / numberOfSketches);
                        }
                        else
                        {
                            defaultSketchSize += (mod / numberOfSketches);
                        }
                    }

                    Vector3D planeNormal = new Vector3D(1, 0, 0);
                    for (int i = 0; i < numberOfSketches; i++)
                    {
                        Point3D minPlanePoint = new Point3D(minPoint.X + (i * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Point3D maxPlanePoint = new Point3D(minPoint.X + ((i + 1) * defaultSketchSize), minPoint.Y, minPoint.Z);
                        Plane minPlane = new Plane(planeNormal, minPlanePoint);
                        Plane maxPlane = new Plane(planeNormal, maxPlanePoint);

                        List<SteelThicknessGaugingPoint> gaugingPointsBySketch = this.GetPointsBetweenPlanes(steelThicknessGaugingPoints, minPlane, maxPlane);
                        if (gaugingPointsBySketch == null || gaugingPointsBySketch.Count == 0)
                        {
                            continue;
                        }

                        pointsPerSketch.Add(index, gaugingPointsBySketch);
                        index++;

                        Vector3D sketchNormal = new Vector3D();
                        foreach (SteelThicknessGaugingPoint gaugingPointBySketch in gaugingPointsBySketch)
                        {
                            sketchNormal += gaugingPointBySketch.PointGeometry.Normal;

                            if (steelThicknessGaugingPoints.Contains(gaugingPointBySketch))
                            {
                                steelThicknessGaugingPoints.Remove(gaugingPointBySketch);
                            }
                        }
                        sketchNormal /= gaugingPointsBySketch.Count;
                        sketchNormal.Normalize();

                        List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, minPlane, maxPlane, gaugingPointsBySketch, sketchNormal);

                        // Generate thumbnail
                        section.SectionDrawer.ConfigureView(this.glPanel, section.GetDefaultView(), true);
                        section.SectionDrawer.UpdateLastCamera(this.glPanel);

                        Bitmap thumbnail = originalThumbnail.Clone() as Bitmap;

                        Rectangle rectangle = this.GetRectangleOfRenderizedArea(sectionElements);
                        for (int j = 0; j < thumbnail.Width; j++)
                        {
                            for (int k = 0; k < thumbnail.Height; k++)
                            {
                                if (this.IsPixelOfRectangle(j, k, rectangle, 3))
                                {
                                    thumbnail.SetPixel(j, k, Color.Red);
                                }
                            }
                        }

                        string thumbName = thumbnail.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                        this.bitmapCount++;

                        thumbName = document.HDF5DataAccessController.SaveBitmap(thumbnail, thumbName);
                        bitmaps.Add(thumbName);

                        C4DView view = section.SectionDrawer.ComputeViewForSectionElements(this.glPanel, sectionElements, false, 1.2f, true);
                        if (view == null)
                        {
                            view = section.GetDefaultView();
                        }

                        section.SectionDrawer.ConfigureView(this.glPanel, view, false);
                        section.SectionDrawer.UpdateLastCamera(this.glPanel);

                        List<int> pointIDs = new List<int>();
                        foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in gaugingPointsBySketch)
                        {
                            pointIDs.Add(steelThicknessGaugingPoint.ID);
                        }

                        section.SectionDrawer.SetPriorityVisibleElements("SteelThicknessGaugingPoint", pointIDs);

                        Bitmap bitmap = section.SectionDrawer.DrawModelImmediateModeCuttingOfCompartment(this.glPanel);

                        section.SectionDrawer.ClearPriorityVisibleElements("SteelThicknessGaugingPoint");

                        string bitmapName = bitmap.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                        this.bitmapCount++;

                        bitmapName = document.HDF5DataAccessController.SaveBitmap(bitmap, bitmapName);
                        bitmaps.Add(bitmapName);

                        bitmap.Dispose();
                    }

                    #endregion
                }
                else if (section.SectionType.Name.Equals("AnyOtherSection"))
                {
                    #region Any Other Section

                    pointsPerSketch.Add(index, steelThicknessGaugingPoints);
                    index++;

                    List<SectionElement> sectionElements = this.GetSectionElementsVisibleInSketch(section, steelThicknessGaugingPoints);

                    Bitmap thumbnail = originalThumbnail.Clone() as Bitmap;

                    Rectangle rectangle = this.GetRectangleOfRenderizedArea(sectionElements);
                    for (int i = 0; i < thumbnail.Width; i++)
                    {
                        for (int j = 0; j < thumbnail.Height; j++)
                        {
                            if (this.IsPixelOfRectangle(i, j, rectangle, 3))
                            {
                                thumbnail.SetPixel(i, j, Color.Red);
                            }
                        }
                    }

                    //int newWidht = (int)(thumbnail.Width / 5);
                    //int newHeight = (int)(thumbnail.Height / 5);
                    //Image image = thumbnail.GetThumbnailImage(newWidht, newHeight, null, IntPtr.Zero);
                    //thumbnail = new Bitmap(image);

                    string thumbName = thumbnail.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                    this.bitmapCount++;

                    thumbName = document.HDF5DataAccessController.SaveBitmap(thumbnail, thumbName);
                    bitmaps.Add(thumbName);

                    //section.SectionDrawer.CurrentViewModeName = "SketchFormat";

                    C4DView view = section.SectionDrawer.ComputeViewForSectionElements(this.glPanel, sectionElements, false);
                    if (view == null)
                    {
                        view = section.GetDefaultView();
                    }

                    section.SectionDrawer.ConfigureView(this.glPanel, view, false);
                    section.SectionDrawer.UpdateLastCamera(this.glPanel);

                    // Get IDs of gauging points to set in SectionDrawer
                    List<int> pointIDs = new List<int>();
                    foreach (SteelThicknessGaugingPoint steelThicknessGaugingPoint in steelThicknessGaugingPoints)
                    {
                        pointIDs.Add(steelThicknessGaugingPoint.ID);
                    }

                    section.SectionDrawer.SetPriorityVisibleElements("SteelThicknessGaugingPoint", pointIDs);

                    Bitmap bitmap = section.SectionDrawer.DrawModelImmediateModeCuttingOfCompartment(this.glPanel);

                    section.SectionDrawer.ClearPriorityVisibleElements("SteelThicknessGaugingPoint");

                    string bitmapName = bitmap.RawFormat.Guid.ToString() + "-" + this.bitmapCount.ToString() + ".jpg";
                    this.bitmapCount++;

                    bitmapName = document.HDF5DataAccessController.SaveBitmap(bitmap, bitmapName);
                    bitmaps.Add(bitmapName);

                    bitmap.Dispose();

                    #endregion
                }

                section.SectionDrawer.CurrentViewModeName = sectionDrawerCurrentViewModeName;

                return bitmaps;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Sketch for section/campaign report cannot be created!", ex);
            }
        }
 public List<string> CreateSketch(Section section, Section symmetricSection, CampaignReport campaignReport, List<SteelThicknessGaugingPoint> steelThicknessGaugingPoints)
 {
     SortedList<int, List<SteelThicknessGaugingPoint>> pointsPerSketch;
     return this.CreateSketch(section, symmetricSection, campaignReport, steelThicknessGaugingPoints, out pointsPerSketch);
 }
        private void ToAverageInLongitudinalSection(Section section1, Section section2, CampaignReport sourceCampaignReport, ref int initialIacsID)
        {
            try
            {
                if (section1 == null && section2 == null)
                    return;

                Dictionary<string, Dictionary<ISectionElement, List<IGaugingElement>>> gaugingPointsByStructureElementTypeInSection1 = null;
                Dictionary<string, Dictionary<ISectionElement, List<IGaugingElement>>> gaugingPointsByStructureElementTypeInSection2 = null;

                if (section1 != null)
                {
                    gaugingPointsByStructureElementTypeInSection1 = section1.GetGaugingElementsBySectionElementInCampaignReport("SteelThicknessGaugingPoint", sourceCampaignReport);
                }
                if (section2 != null)
                {
                    gaugingPointsByStructureElementTypeInSection2 = section2.GetGaugingElementsBySectionElementInCampaignReport("SteelThicknessGaugingPoint", sourceCampaignReport);
                }

                List<SteelThicknessGaugingPoint> averageGaugingPointsInSection1 = null;
                List<SteelThicknessGaugingPoint> averageGaugingPointsInSection2 = null;

                this.averageGaugingsCreator.CreateAverageGaugingPointsInLongitudinalSection(section1, section2, gaugingPointsByStructureElementTypeInSection1, gaugingPointsByStructureElementTypeInSection2, out averageGaugingPointsInSection1, out averageGaugingPointsInSection2);

                this.AddAverageGaugingPointsInSection(section1, averageGaugingPointsInSection1);
                this.AddAverageGaugingPointsInSection(section2, averageGaugingPointsInSection2);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Campaign cannot be converted to IACS in Symmetric Longitudinal Section!", ex);
            }
        }
        private void ToAverage(CampaignReport sourceCampaignReport, int barStep)
        {
            try
            {
                Vessel vessel = sourceCampaignReport.Campaign.Vessel;
                Document document = vessel.Document as Document;

                string campaignTypeName = "SteelThicknessGaugingCampaign";
                CampaignReport averageCampaignReport = document.AddNewCampaignReport(this.averageCampaign, campaignTypeName, false);
                averageCampaignReport.Name = this.entitiesResourceManager.GetString("AverageCampaignInitialName") + " " + sourceCampaignReport.Name;
                averageCampaignReport.SourceCampaignReportID = sourceCampaignReport.ID;
                averageCampaignReport.SourceCampaignReportGUID = sourceCampaignReport.GUID;

                this.currentAverageCampaignReport = averageCampaignReport;

                SortedList<int, SortedList<string, IList>> sectionElements = document.GetSectionsPermanentElementsInCampaignReport(sourceCampaignReport);

                // List to control sections already computed
                List<int> sectionIDs = new List<int>(sectionElements.Keys);

                int index = 1;
                int totalSections = sectionIDs.Count;

                foreach (int sectionID in sectionElements.Keys)
                {
                    if (!sectionIDs.Contains(sectionID))
                    {
                        continue;
                    }

                    Section section = vessel.GetSection(sectionID);
                    if (section == null || section.SectionType == null || section.SectionType.IsNameNull)
                    {
                        continue; // Exception?
                    }

                    int initialIacsID = this.DefineInitialIACSID(section) + 1;

                    this.progressBar.Bar.Value += barStep;
                    this.progressBar.Detail.Text = "(" + index.ToString() + "/" + totalSections.ToString() + ") " + section.Name;
                    this.progressBar.Show();
                    this.progressBar.BringToFront();
                    this.progressBar.Refresh();

                    index++;

                    // ToDo: Think in a correct solution
                    this.epsilon = (section.BoundingBox.YMax - section.BoundingBox.YMin) * 0.02;

                    if (!sectionElements[sectionID].ContainsKey("SteelThicknessGaugingPoint"))
                    {
                        continue;
                    }

                    switch (section.SectionType.Name)
                    {
                        case "WebFrames":
                        case "TransverseBulkheads":
                        case "TransverseCentralBulkheads":
                        case "SwashBulkheads":
                        {
                            #region Non Longitudinal

                            this.ToAverageInNonLongitudinalSection(section, sourceCampaignReport, ref initialIacsID);
                            break;

                            #endregion
                        }
                        case "MainDeck":
                        case "OtherDecks":
                        case "InnerBottom":
                        case "Bottom":
                        {
                            #region Decks and Inner Bottom

                            this.ToAverageInDecksSection(section, sourceCampaignReport, ref initialIacsID);
                            break;

                            #endregion
                        }
                        case "Shell":
                        case "Longitudinal":
                        case "LongitudinalBulkheads":
                        case "LongitudinalCentralBulkheads":
                        {
                            #region Symmetric Longitudinal

                            Section symmetricSection = null;
                            if (this.symmetricLongitudinalSections.ContainsKey(sectionID))
                            {
                                symmetricSection = vessel.GetSection(this.symmetricLongitudinalSections[sectionID]);
                                if (symmetricSection != null)
                                {
                                    sectionIDs.Remove(symmetricSection.ID);
                                }
                            }

                            this.ToAverageInLongitudinalSection(section, symmetricSection, sourceCampaignReport, ref initialIacsID);
                            break;

                            #endregion
                        }
                        case "AnyOtherSection":
                        {
                            #region Others Section Types

                            this.ToAverageInOtherTypesSection(section, sourceCampaignReport, ref initialIacsID);
                            break;

                            #endregion
                        }
                    }

                    sectionIDs.Remove(sectionID);
                    document.ReApplyCampaigns(section);
                }

                document.DocumentStateController.BroadcastEntityCreation(document, averageCampaignReport);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Campaign cannot be converted to IACS!", ex);
            }
        }
        private bool IsAverageCampaignReportAlreadyCreated(CampaignReport campaignReport, out Campaign createdIACSCampaign)
        {
            try
            {
                createdIACSCampaign = null;
                Campaign campaign = campaignReport.Campaign;

                List<Campaign> steelThicknessGaugingCampaigns = campaign.Vessel.GetCampaigns("SteelThicknessGaugingCampaign");
                foreach (Campaign steelThicknessGaugingCampaign in steelThicknessGaugingCampaigns)
                {
                    if (!steelThicknessGaugingCampaign.IsConvertedCampaign)
                    {
                        continue;
                    }

                    if (steelThicknessGaugingCampaign.SourceCampaignID != campaign.ID)
                    {
                        continue;
                    }

                    createdIACSCampaign = steelThicknessGaugingCampaign;

                    foreach (CampaignReport steelThicknessGaugingCampaignReport in steelThicknessGaugingCampaign.CampaignReports.Values)
                    {
                        if (steelThicknessGaugingCampaignReport.SourceCampaignReportID == campaignReport.ID)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("IsCampaignAlreadyCreated cannot be executed!", ex);
            }
        }
        public List<string> CreateSketch(Section section, Section symmetricSection, CampaignReport campaignReport)
        {
            try
            {
                if (section == null)
                {
                    throw new ArgumentNullException("section");
                }

                if (symmetricSection == null)
                {
                    throw new ArgumentNullException("symmetricSection");
                }

                if (campaignReport == null)
                {
                    throw new ArgumentNullException("campaignReport");
                }

                if (!section.SectionType.Name.Equals("Shell") && !section.SectionType.Name.Equals("Longitudinal") &&
                    !section.SectionType.Name.Equals("LongitudinalBulkheads") && !symmetricSection.SectionType.Name.Equals("Shell") &&
                    !symmetricSection.SectionType.Name.Equals("Longitudinal") && !symmetricSection.SectionType.Name.Equals("LongitudinalBulkheads")
                    )
                {
                    return null;
                }

                SortedList<string, IList> sectionElementsInCampaign = section.GetPermanentElementsInCampaignReport(campaignReport);
                SortedList<string, IList> symmetricElementsInCampaign = symmetricSection.GetPermanentElementsInCampaignReport(campaignReport);

                List<SteelThicknessGaugingPoint> sectionSteelThicknessGaugingPoints = (sectionElementsInCampaign.ContainsKey("SteelThicknessGaugingPoint")) ? sectionElementsInCampaign["SteelThicknessGaugingPoint"].Cast<SteelThicknessGaugingPoint>().ToList() : null;
                List<SteelThicknessGaugingPoint> symmetricSteelThicknessGaugingPoints = (symmetricElementsInCampaign.ContainsKey("SteelThicknessGaugingPoint")) ? symmetricElementsInCampaign["SteelThicknessGaugingPoint"].Cast<SteelThicknessGaugingPoint>().ToList() : null;

                List<SteelThicknessGaugingPoint> steelThicknessGaugingPoints = new List<SteelThicknessGaugingPoint>();
                if (sectionSteelThicknessGaugingPoints != null && sectionSteelThicknessGaugingPoints.Count > 0)
                {
                    steelThicknessGaugingPoints.AddRange(sectionSteelThicknessGaugingPoints);
                }
                if (symmetricSteelThicknessGaugingPoints != null && symmetricSteelThicknessGaugingPoints.Count > 0)
                {
                    steelThicknessGaugingPoints.AddRange(symmetricSteelThicknessGaugingPoints);
                }

                SortedList<int, List<SteelThicknessGaugingPoint>> pointsPerSketch;
                return this.CreateSketch(section, symmetricSection, campaignReport, steelThicknessGaugingPoints, out pointsPerSketch);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Sketch for section/campaign report cannot be created!", ex);
            }
        }
        public bool ToAverage(CampaignReport sourceCampaignReport)
        {
            try
            {
                #region verify parameters

                if (sourceCampaignReport == null)
                {
                    throw new ArgumentNullException("sourceCampaignReport");
                }

                if (!sourceCampaignReport.Campaign.CampaignType.IsGauging)
                {
                    throw new ArgumentException("Campaign must be gauging campaign'");
                }

                #endregion

                Campaign sourceCampaign = sourceCampaignReport.Campaign;
                Vessel vessel = sourceCampaign.Vessel;
                Document document = vessel.Document as Document;

                Campaign createdAverageCampaign = null;
                if (this.IsAverageCampaignReportAlreadyCreated(sourceCampaignReport, out createdAverageCampaign))
                {
                    MessageBox.Show(UserMessage.Show("Command.ConvertToAverageCommand.CampaignAlreadyConvertedMessage", "This campaign/campaign report is already converted to these parameters."),
                                                        UserMessage.Show("Command.ConvertToAverageCommand.ProgressBarTitle", "Convert campaign/campaign report to Average"),
                                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }

                List<Section> sections = document.GetSectionsWithPermanentElementsInCampaignReport(sourceCampaignReport);
                int countSections = sections.Count;

                if (countSections == 0)
                {
                    MessageBox.Show(UserMessage.Show("Command.ConvertToAverageCommand.NotElementsToConvert", "This campaign/campaign report cannot be converted because is has not any element."),
                                    UserMessage.Show("Command.ConvertToAverageCommand.ProgressBarTitle", "Convert campaign/campaign report to Average"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    return false;
                }

                #region Showing the progress bar

                this.progressBar = new Cad4DProgressBar();
                this.progressBar.Text = "";
                this.progressBar.Title.Text = UserMessage.Show("Command.ConvertToAverageCommand.ProgressBarTitle", "Convert campaign to Average");
                this.progressBar.Detail.Text = "";
                this.progressBar.Message.Text = UserMessage.Show("Command.ConvertToAverageCommand.ProgressBarMessage", "Converting campaign to Average. Please wait...");
                this.progressBar.Bar.Minimum = 0;
                this.progressBar.Bar.Maximum = 100;
                this.progressBar.Bar.Value = 0;
                this.progressBar.TopMost = true;
                this.progressBar.Show();
                this.progressBar.BringToFront();
                this.progressBar.Refresh();

                #endregion

                document.UpdateDocument(sourceCampaign.Date);

                int barStep = (int)(100 / countSections);

                bool isAlreadyCreatedAverageCampaign = createdAverageCampaign != null;
                if (!isAlreadyCreatedAverageCampaign)
                {
                    this.CreateAverageCampaign(sourceCampaign);
                    if (this.averageCampaign == null)
                    {
                        return false;
                    }
                }
                else
                {
                    this.averageCampaign = createdAverageCampaign;
                }

                this.symmetricLongitudinalSections = document.GetSymmetricLongitudinalSectionsInCampaignReport(sourceCampaignReport);
                this.ToAverage(sourceCampaignReport, barStep);

                // Change reference date to view gauging points created in IACS campaign
                document.DocumentStateController.DateTimeControl.DateTime = this.averageCampaign.Date;

                progressBar.Bar.Value = 100;
                progressBar.Show();
                progressBar.BringToFront();
                progressBar.Refresh();

                return true;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Campaign cannot be converted to IACS!", ex);
            }
            finally
            {
                #region Finalize progress bar

                if (progressBar != null)
                {
                    progressBar.Bar.Value = 100;
                    progressBar.Close();
                    progressBar.Dispose();
                }

                #endregion
            }
        }
        public List<string> CreateSketch(Section section, CampaignReport campaignReport)
        {
            try
            {
                if (section == null)
                {
                    throw new ArgumentNullException("section");
                }

                if (campaignReport == null)
                {
                    throw new ArgumentNullException("campaignReport");
                }

                SortedList<string, IList> elementsInCampaign = section.GetPermanentElementsInCampaignReport(campaignReport);
                if (!elementsInCampaign.ContainsKey("SteelThicknessGaugingPoint") || elementsInCampaign["SteelThicknessGaugingPoint"].Count == 0)
                {
                    return null;
                }

                SortedList<int, List<SteelThicknessGaugingPoint>> pointsPerSketch;
                List<SteelThicknessGaugingPoint> steelThicknessGaugingPoints = elementsInCampaign["SteelThicknessGaugingPoint"].Cast<SteelThicknessGaugingPoint>().ToList();

                return this.CreateSketch(section, campaignReport, steelThicknessGaugingPoints, out pointsPerSketch);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Sketch for section/campaign report cannot be created!", ex);
            }
        }