/*! Writes the data from \a pattern to a file with the given \a fileName. * Returns \c true if successful, otherwise returns \c false. */ int writePec(EmbPattern* pattern, const char* fileName) { EmbFile* file = 0; if(!embStitchList_count(pattern->stitchList)) { embLog_error("format-pec.c writePec(), pattern contains no stitches\n"); return 0; } /* Check for an END stitch and add one if it is not present */ if(pattern->lastStitch->stitch.flags != END) embPattern_addStitchRel(pattern, 0, 0, END, 1); file = embFile_open(fileName, "wb"); if(!file) { embLog_error("format-pec.c writePec(), cannot open %s for writing\n", fileName); return 0; } embPattern_flipVertical(pattern); /* TODO: There needs to be a matching flipVertical() call after the write to ensure multiple writes from the same pattern work properly */ embPattern_fixColorCount(pattern); embPattern_correctForMaxStitchLength(pattern,12.7, 204.7); embPattern_scale(pattern, 10.0); binaryWriteBytes(file, "#PEC0001", 8); writePecStitches(pattern, file, fileName); embFile_close(file); return 1; }
/*! Adds a stitch to the pattern (\a p) at the absolute position (\a x,\a y). Positive y is up. Units are in millimeters. */ void embPattern_addStitchAbs(EmbPattern* p, double x, double y, int flags, int isAutoColorIndex) { EmbStitch s; if(!p) { embLog_error("emb-pattern.c embPattern_addStitchAbs(), p argument is null\n"); return; } if(flags & END) { if(embStitchList_empty(p->stitchList)) return; /* Prevent unnecessary multiple END stitches */ if(p->lastStitch->stitch.flags & END) { embLog_error("emb-pattern.c embPattern_addStitchAbs(), found multiple END stitches\n"); return; } embPattern_fixColorCount(p); /* HideStitchesOverLength(127); TODO: fix or remove this */ } if(flags & STOP) { if(embStitchList_empty(p->stitchList)) return; if(isAutoColorIndex) p->currentColorIndex++; } /* NOTE: If the stitchList is empty, we will create it before adding stitches to it. The first coordinate will be the HOME position. */ if(embStitchList_empty(p->stitchList)) { /* NOTE: Always HOME the machine before starting any stitching */ EmbPoint home = embSettings_home(&(p->settings)); EmbStitch h; h.xx = home.xx; h.yy = home.yy; h.flags = JUMP; h.color = p->currentColorIndex; p->stitchList = p->lastStitch = embStitchList_create(h); } s.xx = x; s.yy = y; s.flags = flags; s.color = p->currentColorIndex; #ifdef ARDUINO inoEvent_addStitchAbs(p, s.xx, s.yy, s.flags, s.color); #else /* ARDUINO */ p->lastStitch = embStitchList_add(p->lastStitch, s); #endif /* ARDUINO */ p->lastX = s.xx; p->lastY = s.yy; }
/* Adds a stitch at the absolute position (x,y). Positive y is up. Units are in millimeters. */ void embPattern_addStitchAbs(EmbPattern* p, double x, double y, int flags, int isAutoColorIndex) { EmbStitch s; if(!p) { embLog_error("emb-pattern.c embPattern_addStitchAbs(), p argument is null\n"); return; } if(flags & END) { embPattern_fixColorCount(p); /* HideStitchesOverLength(127); TODO: fix or remove this */ } if((flags & STOP) && embStitchList_empty(p->stitchList)) return; if((flags & STOP) && isAutoColorIndex) { p->currentColorIndex++; } s.xx = x; s.yy = y; s.flags = flags; s.color = p->currentColorIndex; if(!(p->stitchList)) { p->stitchList = (EmbStitchList*)malloc(sizeof(EmbStitchList)); if(!p->stitchList) { embLog_error("emb-pattern.c embPattern_addStitchAbs(), cannot allocate memory for p->stitchList\n"); return; } p->stitchList->stitch = s; p->stitchList->next = 0; p->lastStitch = p->stitchList; } else { embStitchList_add(p->lastStitch, s); p->lastStitch = p->lastStitch->next; } p->lastX = s.xx; p->lastY = s.yy; }
/* Adds a stitch at the absolute position (x,y). Positive y is up. Units are in millimeters. */ void embPattern_addStitchAbs(EmbPattern* p, double x, double y, int flags, int isAutoColorIndex) { /* TODO: pointer safety */ EmbStitch s; if(flags & END) { embPattern_fixColorCount(p); /* HideStitchesOverLength(127); TODO: fix or remove this */ } if((flags & STOP) && embStitchList_empty(p->stitchList)) return; if((flags & STOP) && isAutoColorIndex) { p->currentColorIndex++; } s.xx = x; s.yy = y; s.flags = flags; s.color = p->currentColorIndex; if(!(p->stitchList)) { p->stitchList = (EmbStitchList*)malloc(sizeof(EmbStitchList)); /* TODO: malloc fail error */ p->stitchList->stitch = s; p->stitchList->next = 0; p->lastStitch = p->stitchList; } else { embStitchList_add(p->lastStitch, s); p->lastStitch = p->lastStitch->next; } p->lastX = s.xx; p->lastY = s.yy; }